Blog: WordPress
Communication Towers During Sunrise

Add Advanced Custom Field To RSS Feed In WordPress

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown SEO.

One way to move people from being prospects to being customers is to educate them through content marketing.

This usually takes the form of blogging, podcasting, videos, or some other form of serialized content, published on your company blog.

Many companies use their email newsletter as a way to send their RSS feed directly to more inboxes. This means more people are hearing about what your company does, what your company values are, and what makes you different than your competitors.

All good so far — having your blog posts automatically mailed out each day. But sometimes you might want to add something extra to the individual posts in your RSS feed.

We’ve already covered how to add a featured image to your newsletter RSS feed. What if you want to add an image that doesn’t show up on your website, but only in the RSS feed?

What if you want to add extra text that only shows up in the RSS feed?

One way we can accomplish this is by using Advanced Custom Fields and a custom function.

Using Advanced Custom Fields

Advanced Custom Fields is a plugin that many WordPress developers use to add extra fields to specific templates, posts, or pages on websites. There’s a free version and Pro version of this plugin. The Pro gives you more flexibility, but for this tutorial, we’ll only need the regular version.

ACF works by setting up Field Groups that display when certain rules are met. You can add as few or as many custom fields to these Field Groups as you need.

Let’s say we just want to add a place to add an RSS image to each blog post that goes out in our newsletter. Our image will go at the bottom of our regular RSS feed, below the regular content that appears on our website.

Our first task is to install the ACF plugin, activate the plugin, and add a new Field Group. Let’s create a rule that this Field Group only appears when we’re editing a regular blog post.

Now, let’s create a custom field labelled RSS Image that allows us to choose an image. For this tutorial, we’ll select the Image Object, so we can access different types of information associated with our image.

At this point, you should have something similar to the screenshot below.

Screenshot of Advanced Custom Fields, setting up a field group

After saving this, you should have a custom field on your Posts when you edit them. As shown in the screenshot, the Field Name for our RSS Image is rss_image.

Let’s add this custom image to our newsletter RSS feed.

Add A Custom Image To RSS Feed

First the code, then the explanation of what’s going on. Add this snippet to your functions.php file.

 * Add Advanced Custom Field to RSS Feed

function ld_advanced_custom_field_in_feed($content) {  
    if(is_feed()) {  
        $post_id = get_the_ID();  
        $rssimage = get_field('rss_image');

            if( !empty($rssimage) ): 
                $imgurl = $rssimage['url'];
                $imgalt = $rssimage['alt'];
                $imgtitle = $rssimage['title'];

                $output .= '<p class="rss-image"><img src="';
                $output .=  $imgurl;
                $output .= '" alt="';
                $output .= $imgalt;
                $output .= '" title="';
                $output .= $imgtitle;
                $output .= '" /></p>'; 
        $content = $content.$output;  
    return $content;  

Let’s take a look at what’s taking place in this custom function.

The Breakdown

In the first line, we’re creating a custom function that takes one argument, the $content variable.

IN the last line, after our function runs, we’re filtering the post content through our function. In WordPress, filters take action with specific data as it passes between database and browser (or vice versa).

In the first line inside our function, we use an if statement to determine if this output is for an RSS feed or not, using is_feed().

When you look at how the curly brackets are nested, you’ll see that if this isn’t an RSS feed, we simply return the original content. Everything remains the same.

On a side note, if we want to target a specific type of feed, then is_feed will accept that as an argument. For example, if we only want to target the RSS feed for a Post Type of events, we would write if is_feed('events').

Okay, on the next line, we’re getting the specific Post ID for each post and storing it in the $post_id variable. WordPress stores each post in your database with a unique ID.

On the next line, we’re using the Advanced Custom Fields specific function get_field to get the value of our custom field rss_image for each post. We then store that information in the new variable, $rssimage.

When we set up our custom field for the RSS Image, we chose to store it as an Image Object, instead of just a URL, or ID Number. This gives us access to all the information stored with the image, like the Alternative Text, Title, and URL of the image in the Media Library.

We then use another if statement to say, if our variable $rssimage is not empty for this post, so the following. The exclamation point is a symbol that means NOT in PHP.

Now we access the URL, title, and alt text for our image, and store them in separate variables.

Next, we use a variable called $output to store new information about our image. What we’re doing on these next few lines is creating HTML strings and storing them in our variable.

The symbol .= in this case means add the stuff on the right side of the equals sign and store it in the variable on the left side of the equals sign. The period by itself is similar to an addition symbol in PHP. If we used the equals sign by itself, we would be overwriting our variable each new line. This way, we are just adding to the existing value.

Now, our nested if statement is done. We add everything in our $output variable to the end of the $content variable and store it back into the $content variable and return it.

Remember that everything on the right side of the equals sign gets stored in the variable on the left hand side of the equals sign.

Maybe you want to put the RSS Image before our regular content? No problem. Just reverse the order in which you are adding those values together.

$content = $output.$content;

This is only one example of how you can use Advanced Custom Fields to add selected values to your RSS feed. You could easily use the same principles to add an extra text field or other values that are RSS specific.

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown SEO.

4 comments on “Add Advanced Custom Field To RSS Feed In WordPress

  1. Hi Marisa:

    If you already have MailChimp hooked up to your RSS feed (i.e. when you publish a new post, it goes out to your list), then yes, this is what you would want to use.

    – John

  2. Hi John, I cannot retrieve the ACF value on my feed. It’s like the the_content hook is not working properly. Whenever I use var_dump($content) before the return, the ACF value is there. However, when I comment out the var_dump, the ACF value is no longer in the content. Why is that? I hope that you could help me, this would really solve the problem I am facing with my current project. Thank you!

    1. Hi Benedict John:

      The way I understand this hook is that it only adds the ACF field to the_content if it is the RSS feed. It wouldn’t show up otherwise.

      If you are trying to add an image (not the Featured Image) to the top of the RSS feed, reverse the line where you add the output to the content:

      $content = $output.$content;

      If you can send yourself a test email (if you are using MailChimp or another templated email service), you may be able to troubleshoot.


Join the Conversation

Your email address will be kept private. Required fields marked *.