Welcome to the new design! Please report any bugs or issues, thanks :)
Web Dev + WordPress + Security

WordPress RDF Source Makeover

I love looking at beautiful source-code output. However WordPress tends to spit code out in random chunks, often leaving spaces, line breaks, and tabs littered throughout the source output. This messes things up. Lists don’t look like lists and logically written code often appears scattered along the page carelessly. Often, this is the result of poorly written PHP, which can be manipulated to write beautifully aligned code that looks as good as it works.

Note: This article applies specifically to WordPress 2.0.2, but may be generalized to any WP 2.0+ version.

Beautiful Source-Code Output, Part 1: Whip your WordPress RDF Code into Submission

For the first article in this series, we will bring order to the typical RDF chaos that WordPress spits out by default. To see an example of this, check out this source code, taken directly from the output of a default installation of WordPress. Notice how the awkward chunk of code leans, strangely enough, to the right. This type of RDF-code output seems to be the norm for WordPress users who have enabled that particular feature.

Okay, so it looks hideous, and we want to clean it up and make it shine. First thing to do is backup everything, even your database. Don’t trip though — this is a simple “cut-and-paste” procedure, but it is always a good idea to have a recent backup of your data just in case. Next, open the file wp-includes/comment-functions.php and find the function trackback_rdf, which is located about halfway down the page on line #525 (#509 for WP 2.0). Now, replace the following block of PHP code:

function trackback_rdf($timezone = 0) {
	global $id;
	if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
	echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	    xmlns:dc="http://purl.org/dc/elements/1.1/"
	    xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
		<rdf:Description rdf:about="';
	the_permalink();
	echo '"'."\n";
	echo '    dc:identifier="';
	the_permalink();
	echo '"'."\n";
	echo '    dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
	echo '    trackback:ping="'.trackback_url(0).'"'." />\n";
	echo '</rdf:RDF>';
	}
}

..with our new and improved version (download available below):

function trackback_rdf($timezone = 0) {
	global $id;
	if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
	echo '<rdf:RDF ';
	echo "\n";
	echo '   xmlns:dc="http://purl.org/dc/elements/1.1/" ';
	echo "\n";
	echo '   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ';
	echo "\n";
	echo '   xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">';
	echo "\n";
	echo '<rdf:Description ';
	echo "\n";
	echo '   dc:creator="';
	the_author();
	echo '" '."\n";
	echo '   dc:date="';
	the_date_xml();
	echo ' @ ';
	the_time('g:i a');
	echo '" '."\n";
	echo '   dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'" '."\n";
	echo '   rdf:about="';
	the_permalink();
	echo '" '."\n";
	echo '   dc:identifier="';
	the_permalink();
	echo '" '."\n";
	echo '   trackback:ping="'.trackback_url(0);
	echo '" '."\n";
	echo '   dc:description="';
	the_excerpt_rss();
	echo '" />'."\n";
	echo '</rdf:RDF>';
	}
}

Finally, open your WordPress theme’s “index.php” file, find the loop, and add an echoed line break to the commented-out RDF function. Your code should now look like this:

<!--
<?php trackback_rdf(); ?>
<?php echo "\n"; ?>
-->

That’s it — save the two files, upload them, refresh your pages, and check the RDF output in the source code. If all goes well, your RDF code should now resemble this:

<!--
<rdf:RDF 
   xmlns:dc="http://purl.org/dc/elements/1.1/" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description 
   dc:creator="Perishable" 
   dc:date="2006-02-22 @ 11:19 am" 
   dc:title="WordPress RDF Source Makeover" 
   rdf:about="https://perishablepress.com/press/?p=49" 
   dc:identifier="https://perishablepress.com/press/?p=49" 
   trackback:ping="https://perishablepress.com/press/wp-trackback.php?p=49" 
   dc:description="Beautiful Source-Code Output, Part 1: Whip your WordPress RDF Code into Submission
I love looking at beautiful source-code output. [...continued &#187;]" />
</rdf:RDF>
-->

Now that’s much better!

Next time we will look at constructing righteous headings and pimping them for excellence on planet source-code output.

Update

To gain greater control over your WordPress RDF source-code output, replace the native function, the_excerpt_rss(); (located near the last line of the trackback_rdf() function), with the more flexible function, the_content_rss. This function provides five parameters as described in the WordPress Codex:

<?php the_content_rss('more_link_text', strip_teaser, 'more_file', cut, encode_html); ?>

More specifically, here is how we use the function to limit the number of words output in the dc:description attribute, thus optimizing bandwidth while preventing unruly code examples from disrupting document structure (as in the case of JavaScript comments):

the_content_rss('', TRUE, '', 7, 2);

Fin

Download

Jeff Starr
About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

2 responses to “WordPress RDF Source Makeover”

  1. Sweet, never really thought about the beauty of the output. Normally I think of the server-side elements, thanks for this interesting information.

  2. Jeff Starr
    Perishable 2007/03/01 6:55 pm

    Hey thanks for commenting on this article. I find beautifully presented source code quite inspiring, and have been meaning to write more on the topic of writing elegant code for its own sake. Perhaps soon I will find the time to delve into the fine art of producing aesthetically satisfying code output..

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
BBQ Pro: The fastest firewall to protect your WordPress.
Thoughts
This new Admin Page Framework looks very good. Bookmarked for future use.
Two great places to find awesome plugins: pluginsearch.com and WP's browse new.
2 things I hate to see in stylesheets: _ and #
Love VLC media player but it fails miserably when it comes to randomizing large collections of mp3 and other files.
Dashlane redesigned, stating proudly they "removed all filigree". Should have kept it; the app now looks generic and boring. Killed your identity.
Working on integration for setaPDF + EDD on the new books subdomain. Good times.
Toggle visibility of hidden files on Mac: Cmd + Shift + .