You know custom fields? (If not, just take a quick look here…) Unfortunatly I’m not really using them at the moment on this blog, but I’m planning to do so on another one, collaborating with a new iPhone-App. The fields where made, are filled with content anytime, but how to use them? For my needs it’s enough to past them into the RSS-Feed, they don’t really need to be visible at all, just be there to parse it from the XML-Source of the feed. So, how to manage this one? Here is a quick how-to, after that you should be able to add as many field as you want to your feed, and any other meta informations too!
At first, let me tell you, that this is on your own risk because you need to “hack” some WordPress-files. But hey thats the deal if you want to mod your feed!
Part 1: Finding the source
Lets go: Grab a FTP-Tool of your choice and log in to your server. In this one, navigate to the place where you put all the WordPress-files. Navigate to “wp-includes” and find the .php-files starting with “f”. You will find multiple ones, all starting with “feed-….”. Thats the point where our way splits into 3 different ways: As you know, there is RSS, RSS2 and also the Atom format for feeds. Depending on which format you want to hand to your customers you need to select the matching file, the rest should still be the same. I’ll go on with the “feed-atom.php” file. Open the file in an editor of your choice. Now you will see a lot of code, depending on your skills in PHP and XML you won’t really understand anything but you don’t really need to even if it can be a little helpful for the following stuff.
Part 2: Starting to hack
Take a quick look of the opened file and find the
<entry>
...
</entry>
tag. Just if you are interested, at the moment mine looks like this:
<entry>
<author>
<name><?php the_author() ?></name>
<?php $author_url = get_the_author_meta('url'); if ( !empty($author_url) ) : ?>
<uri><?php the_author_meta('url')?></uri>
<?php endif; ?>
</author>
<title type="<?php html_type_rss(); ?>"><![CDATA[<?php the_title_rss() ?>]]></title>
<link rel="alternate" type="text/html" href="<?php the_permalink_rss() ?>" />
<id><?php the_guid(); ?></id>
<updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
<published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
<?php the_category_rss('atom') ?>
<summary type="<?php html_type_rss(); ?>"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
<?php if ( !get_option('rss_use_excerpt') ) : ?>
<content type="<?php html_type_rss(); ?>" xml:base="<?php the_permalink_rss() ?>"><![CDATA[<?php the_content_feed('atom') ?>]]></content>
<?php endif; ?>
<?php atom_enclosure(); ?>
<?php do_action('atom_entry'); ?>
<link rel="replies" type="text/html" href="<?php the_permalink_rss() ?>#comments" thr:count="<?php echo get_comments_number()?>"/>
<link rel="replies" type="application/atom+xml" href="<?php echo get_post_comments_feed_link(0,'atom') ?>" thr:count="<?php echo get_comments_number()?>"/>
<thr:total><?php echo get_comments_number()?></thr:total>
</entry>
Let’s get ready to rumble! At first, we want to take a quick look at the WordPress-Codex how to access the database containing the postmeta. The Codex works like a wiki, you all should know. Search for “get_post_meta” and take a quick look at this one. As you hopefully will see this function is the one we need to use. Add the following lines inside the “<entry>” tag:
{code codetype=xml}
<!– this is what we add –>
<pricedrop><?php echo get_post_meta($post->ID,’pricedrop’,1); ?></pricedrop>
<!– …more fields below… –>
{/code}
For my needs I decided it would be the best to get the meta-informations all summed up at the end of each entry in the feed, unless they are not visible to the customers. I just added the codesnippet after the “content”-section. Now my <entry> looks like this one:
{code codetype=xml}
<entry>
<author>
<name><?php the_author() ?></name>
<?php $author_url = get_the_author_meta(‘url’); if ( !empty($author_url) ) : ?>
<uri><?php the_author_meta(‘url’)?></uri>
<?php endif; ?>
</author>
<title type=”<?php html_type_rss(); ?>”><![CDATA[<?php the_title_rss() ?>]]></title>
<link rel=”alternate” type=”text/html” href=”<?php the_permalink_rss() ?>” />
<id><?php the_guid(); ?></id>
<updated><?php echo get_post_modified_time(‘Y-m-d\TH:i:s\Z’, true); ?></updated>
<published><?php echo get_post_time(‘Y-m-d\TH:i:s\Z’, true); ?></published>
<?php the_category_rss(‘atom’) ?>
<summary type=”<?php html_type_rss(); ?>”><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
<?php if ( !get_option(‘rss_use_excerpt’) ) : ?>
<content type=”<?php html_type_rss(); ?>” xml:base=”<?php the_permalink_rss() ?>”><![CDATA[<?php the_content_feed('atom') ?>]]></content>
<?php endif; ?>
<?php atom_enclosure(); ?>
<?php do_action(‘atom_entry’); ?>
<!– this is what we add –>
<pricedrop><?php echo get_post_meta($post->ID,’pricedrop’,1); ?></pricedrop>
<!– …more fields below… –>
<link rel=”replies” type=”text/html” href=”<?php the_permalink_rss() ?>#comments” thr:count=”<?php echo get_comments_number()?>”/>
<link rel=”replies” type=”application/atom+xml” href=”<?php echo get_post_comments_feed_link(0,’atom’) ?>” thr:count=”<?php echo get_comments_number()?>”/>
<thr:total><?php echo get_comments_number()?></thr:total>
</entry>
{/code}
Finished….Did you know that it was that easy?
Part 3: Digging deeper
The “<pricedrop>” tag can replaced with any other you want it’s just used to make the field we added better visible when viewing the plain source-code of our feed. Just some more words about using the get_post_meta method: You probably took a look at the equivalent codex article, then just skip this one. Otherwise take a quick look here. As you see, this function got three parameters passed in its call, each seperated through a comma. The first one passes the id of the article which is posted to the feed, this one is the most essential because the id is needed to access the unique key-value-pair in the WordPress-Database. Without this or with an ID not matching the current article the meta informations gathered from the custom fields would be wrong or even just not there. The second one is nearly as essential: WordPress allows you to create multiple custom fields for each article. The code you pasted above gets WordPress to identify the article you want to access but now which of the (possible) multiple custom fields. Because of that the second parameter needs to contain the exact name of the desired custom field, in my example I took “pricedrop”. Last but not least the third parameter is optional intending it is not that important: You decide if you want a single result returned, like a single picture, or even a whole array, like a gallery of pictures. In most of the times you can simply ignore this one.
Okay, thats what I had to say. I’m hoping you can use my snippets of code and feel inspired to mod your feed like you need it – you really got the tools by now!
Share on Facebook