Family Best Time >> Electronics

How to create custom RSS feeds in WordPress

WordPress comes with built-in default RSS feeds. You can modify the default feeds by adding custom content to your RSS feeds, or even adding a post thumbnail to your RSS feeds. The default RSS and Atom feeds are sufficient for most users, but you may want to create a custom RSS feed to deliver a specific type of content. In this article, we will show you how to create custom RSS feeds in WordPress.

Please note that this tutorial is not intended for beginner level WordPress users. If you are a beginner, and still want to give it a try, do it on a local install.

As always, you should create a full backup of your WordPress website before making any major changes to a live website.

With that said, let's get started with your first custom RSS feed in WordPress.

Suppose you want to create a new RSS feed that displays only the following information:

  • Title
  • Link
  • Publication Date
  • Author
  • Excerpt

The first thing you need to do is create the new RSS feed in your theme funciones.php file or in a site-specific plugin:

 add_action ('init', 'customRSS'); función customRSS () add_feed ('feedname', 'customRSSFunc'); 

The above code activates the customRSS Function, which adds the feed. The add_feed function has two arguments, feedname and a callback function. The feedname will create your new feed URL yourdomain.com/feed/feedname and the callback function will be called to actually create the feed. Make a note of the feed name, as you'll need it later.

Once you've initialized the font, you'll need to create the callback function to produce the required font, using the following code in your theme funciones.php file or in a site-specific plugin:

 función customRSSFunc () get_template_part ('rss', 'feedname'); 

The code above is using the get_template_part function to link to a separate template file, however you can also place the RSS code directly in the function. By using get_template_part , We can keep the functionality separate from the design. the get_template_part The function has two arguments, slug and name, which will look for a template file with the name in the following format, starting with the file at the top (if it doesn't find the first, it will move to the second, and soon):

  1. wp-content / themes / child / rss-feedname.php
  2. wp-content / themes / parent / rss-feedname.php
  3. wp-content / themes / child / rss.php
  4. wp-content / themes / parent / rss.php

For the purposes of this tutorial, it's best to set the slug to the type of feed you're creating (in this case:rss), and the name to the feed name set above.

Once you've told WordPress to look for the feed template, you'll need to create it. The following code will produce the font layout with the information we listed above. Save this file in your theme folder as the template file slug-name.php set to get_template_part function.

   - Alimentar   

This template code will generate an RSS feed following the design above. the recuento de entradas The variable allows you to control the number of posts that will be displayed in your feed. The template can be modified as needed to display the information you need (eg post images, comments, etc.).

the the_excerpt_rss The function will display the excerpt of each post, and for posts that don't have excerpts, it will display the first 120 words of the post content.

Finally, to display your feed, you will first need to clean up your WordPress rewrite rules. The easiest way to do this is to log in to your WordPress admin and click on Settings -> Permalinks . Once here, simply click Save Changes , which will clear the rewrite rules.

You can now access your new feed at yourdomain.com/feed/feedname , where feedname was the feedname you gave in the add_feed work sooner.

The W3C offers a feed validation service, which allows you to validate the resulting feed.

Troubleshooting

  • I'm getting a 404 error when trying to view my feed!
    • Check if you are using the correct feed name in your URL. It has to be the one you supplied in add_feed function
    • If you have the correct font name, the rewrite rules may not have been flushed correctly. Please re-save your permalinks just to be safe.
    • If you've resaved your permalinks, you can force a flush rewrite via your theme's functions.php file. Add the following code to the customRSS function we created earlier. Make sure to add the code after the add_feed function.
    •  global $ wp_rewrite; $ wp_rewrite-> flush_rules (); 
    • Once you've added this, reload your WordPress site. NOTE:This must be removed immediately after use. Once is enough for the rules to be removed.
  • My feed is not validating!
    • Using the W3C Source Validator, specific details must be given where your source is not being validated. Edit the feed template file to resolve these issues
  • I'm getting a validation error!
    • This is common when the RSS language has not been set in your WordPress installation. To do this you can add the following code to your theme funciones.php File, to update the language option..
    •  function rssLanguage () update_option ('rss_language', 'en'); add_action ('admin_init', 'rssLanguage'); 
    • Edit the second argument of the update_option function to change the language to one you need. Take a look at the full list of RSS language codes.
    • Once the above code has been added to your functions file, load the WordPress admin screen for it to take effect. After this, the code should be removed from your WordPress functions file. Loading it once is enough to configure the rss_language setting.
    • This can also be done directly in the database, by looking for the rss_language option in the wp_options table.

We hope this article has helped you create your own custom RSS feeds in WordPress. Let us know how and why you are going to use custom RSS feeds on your WordPress site by leaving a comment below.