How to Fetch and Show Posts from a RSS Feed in WordPress

RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.

If you would like to use RSS feed of some other site to show their latest posts into your site/blog then WordPress has a handy function which you can use to pull the posts from another site.

Following code helps you fetch posts from an external RSS feed to display on your site.

    <?php // Get RSS Feed(s) include_once( ABSPATH . WPINC . '/feed.php' ); // Get a SimplePie feed object from the specified feed source. $rss = fetch_feed( 'http://wphelpguide.com/feed/' ); // enter your rss feed url here $maxitems = 3; if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly // Figure out how many total items there are, but limit it to 5. $maxitems = $rss->get_item_quantity( 3 ); 
    
        // Build an array of all the items, starting with element 0 (first element).
        $rss_items = $rss->get_items( 0, $maxitems );
    
    endif;
    ?>

In above code, you first include the WordPress Core file called feed.php which enables the use of fetch_feed function to be used in your file. Then you enter the feed URL in the next line and then define the number of posts you would like to fetch. I would suggest to enter number not more than 10 as fetching large number of posts can slow down your site. After that the function checks the fetched quantity and makes sure function has pulled required number of posts from the RSS feed.

After that posts following code to show the fetched RSS feed output

    

<ul>
        <?php if ( $maxitems == 0 ) : ?>
            

<li><?php _e( 'No items', 'my-text-domain' ); ?></li>


        <?php else : ?>
            <?php // Loop through each feed item and display each item as a hyperlink. ?>
            <?php foreach ( $rss_items as $item ) : ?>        
                

<li>
                    <a target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>"
                        title="<?php printf( __( 'Posted {37256cf0ee31db86add5b6eba3983cfb5fab0b039501789ef81a53d1a467d5f8}s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                        <?php echo $item->get_content(); ?>


<div style="clear:both;"></div>


                        <?php echo esc_html( $item->get_title() ); ?>
                    </a>
                </li>


            <?php endforeach; ?>
        <?php endif; ?>
    </ul>


This code first checks whether or not the posts are pulled and if correct number of posts are pulled then it shows the output. You can customize the output as per your requirement and include and exclude the elements which you want to show.

By default WordPress caches the RSS feed output for 12 hours, which means even if the external site has updated their RSS Feed, WordPress won’t show newer posts until 12 hours have passed. To change that value you can use another filter called wp_feed_cache_transient_lifetime to customize the default time. Paste following code into your theme’s functions.php file and edit the seconds numbers as per your requirement.

function return_cache_time( $seconds ) {
// change the default feed cache recreation period to 2 hours
return (int) 600;  time is given in seconds.. default is 43200
}
 
//set feed cache duration
add_filter( 'wp_feed_cache_transient_lifetime', 'return_cache_time');

How to Add Unique Image Class in WordPress

If you want to dynamically add your own class to every image added from WordPress Editor then use following code. Add the code into your theme’s functions.php file and change the class according to your requirement.

function wphe_image_class_filter($classes) {
	return $classes . ' your_class_name_here'; //make sure to add a space in front of the class name
}
add_filter('get_image_tag_class', 'wphe_image_class_filter');

How to Bulk Remove Featured Images in WordPress

In WordPress, you can only remove post featured image one by one by editing each post and clicking on the “Remove Featured Image” link, which  is easier to do if you have only few posts but if you have hundreds of posts then removing them one by one can be a daunting task.  But fear not, there is a simpler and easier way to bulk remove  featured image in WordPress.

Copy the following code and paste it in your theme’s functions.php file.

global $wpdb;
$wpdb->query( "
    DELETE FROM $wpdb->postmeta 
    WHERE meta_key = '_thumbnail_id'
" );

Note: This code will only remove the featured image from the posts, It will not delete the images from your Media Library or from your uploads folder.

Copy the code and open your site’s Dashboard  and it will immediately remove all the featured image.  After that, please go back to your theme’s functions.php and remove the code from the file or else it will keep removing the featured images afterwards.

Hope this article helped you.

How to Show Default Image if Featured Image is not Added

The “the_post_thumbnail()” function for featured image in WordPress does not shows anything if a featured image is not attached to the post. In case you want to show a default image in your theme if a user has not attached any featured image then use following script:

     <a href="<?php the_permalink(); ?>"> // adds a hyperlink to the image
        <?php
        if ( has_post_thumbnail() ) { //check if featured image is added or not
            the_post_thumbnail( 'medium', array( 'class' => 'img-responsive' )  );  //echos featured image if added
        } 
        else {
        ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/defaultimg.png" class="img-responsive" alt="">  // outputs default image if not . change the url to match your image.                              
        <?php } ?>
    </a>                            

How to Hide Admin Bar for Logged in Users

If you have a WordPress site where end users have to login and you do not want to show the Admin bar to those users then you can disable the Admin bar globally for all users by adding following line of code into your theme’s functions.php file.

//Disable WordPress Admin Bar for all users. */
show_admin_bar(false);

If you want to disable the Admin Bar for end user but still want to show it for Administrator then use following code:

//Disable WordPress Admin Bar for all users except admins */
function wphelp_remove_adminbar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}
add_action('after_setup_theme', 'wphelp_remove_adminbar');

Change WordPress Login Page Logo

Ever wanted to change the default WordPress logo from the wp-login.php page? Well you can do that easily. Copy following snippet and paste it in your theme’s functions.php file and edit the URL of the image.


// login page logo

function wphelp_login_logo() { ?>

<style type="text/css">
	h1 a {
		background-image: url(<?php echo get_template_directory_uri(); ?>/img/text-logo.png) !important;
		width:300px !important;
		height:100px !important;
		background-size: 300px!important;
		}			
</style>

<?php
}
add_action('login_head', 'wphelp_login_logo');