WordPress RDF Source Makeover
Beautiful Source-Code Output, Part 1: Whip your WordPress RDF Code into Submission
Update: This article applies specifically to WordPress 2.0.2, but may be generalized to any WP 2.0+ version.
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.
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('--', '--', wptexturize(strip_tags(get_the_title()))).'"'."\n";
echo ' trackback:ping="'.trackback_url(0).'"'." />\n";
echo '</rdf:RDF>';
}
}
with our new and improved version1:
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('--', '--', 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="http://perishablepress.com/press/?p=49"
dc:identifier="http://perishablepress.com/press/?p=49"
trackback:ping="http://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 »]" />
</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
References
Related articles
- WordPress Notes Plus
- Permalink Enlightenment
- Title Attributes for WordPress Post Navigation
- Reversing WordPress Page Navigation Order
- Passing Quotation Marks via wp_link_pages
- WordPress Core File Edits at Perishable Press
- Execute External WordPress Functions
About this article
This is article #49, posted by Perishable on Wednesday, February 22, 2006 @ 11:19am. Categorized as Websites, WordPress, and tagged with code, customize, php, rdf, source, tweaks, WordPress, xhtml, xml. Updated on December 11, 2007. Visited 17309 times. 2 Responses »
Bookmark • Subscribe • Explore
« Monzilla Media Service Menu • Up • Welcome to Perishable Press »
1 • February 27, 2007 at 5:58 pm — Patrick says:
Sweet, never really thought about the beauty of the output. Normally I think of the server-side elements, thanks for this interesting information.