It is time to upgrade your browser! This site looks and acts much better in a newer browser. Go TO THIS PAGE to find LINKS to all the major NEW BROWSERS. The links are at the bottom of the page under "What can I do?"

Or, you may keep reading this ugly page.

the MTAA-RR

[splash image]

MTAA-RR » news » twhid » geek » create 2 feeds for different media formats in wordpress 1 5 quick and dirty:

Feb 12, 2006

Create 2 feeds for different media formats in Wordpress 1.5 (quick and dirty)

posted at 16:39 GMT by T.Whid in /news/twhid/geek

When linking to media files in WP 1.5.2 the default behavior is to create 2 enclosures for that particular item in the RSS 2.0 feed. My quick and dirty little hack allows me to generate 2 separate feeds for two different media formats. It’s not a plugin for Wordpress and isn’t easily extended without some knowledge of PHP.

What I did is fairly simple and specific to my needs. First, in wp-rss2.php I check for a query string with the media format I want in the feed:
if ( isset($_GET['format']) ) {
	$enc_format = 1;
} else {
	$enc_format = 0;
}

Note: This needs to go after the if (empty($feed)) { etc } bit.

Then later in the same file I decide whether I should pass the format to the rss_enclosure() function. If there is a format specified we pass it to the function, if there isn’t, we just pass false:
if ( $enc_format ) { 
	rss_enclosure($_GET['format']);
} else { 
	rss_enclosure(false);
}

The official WP version of rss_enclosure() doesn’t take an argument, I edited that a bit so that it would take an argument. It is in the file wp-includes/feed-functions.php:
function rss_enclosure($input)
{
global $id, $post;
if (!empty($post->post_password) &&¬
($_COOKIE['wp-postpass_'.COOKIEHASH] ¬
!= $post->post_password)) return;

if( is_array( $custom_fields ) ) 
{
while( list( $key, $val ) = each( $custom_fields ) ) { 
if( $key == 'enclosure' ) {
if (is_array($val)) {
foreach($val as $enc) {
$enclosure = split( "\n", $enc );
if ( $input ) 
{
if ( $input == "wmv" && trim( $enclosure[ 2 ] ) ¬
== "video/x-ms-wmv" ) {
print "<enclosure url='".trim¬
( htmlspecialchars($enclosure[ 0 ])¬
 )."' length='".trim( $enclosure[ 1 ] )."' ¬
type='".trim( $enclosure[ 2 ] )."'/>\n";
}
if ( $input == "m4v" && trim( $enclosure[ 2 ] ) ¬
== "video/quicktime" ) {
print "<enclosure url='".trim¬
( htmlspecialchars($enclosure[ 0 ])¬
)."' length='".trim( $enclosure[ 1 ] )."' ¬
type='".trim( $enclosure[ 2 ] )."'/>\n";
}
}
else
{
print "<enclosure url='".trim¬
( htmlspecialchars($enclosure[ 0 ])¬
)."' length='".trim( $enclosure[ 1 ] )."' ¬
type='".trim( $enclosure[ 2 ] )."'/>\n";
}}}}}}}

You can see that I only check for WMV or M4V, eg, if ( $input == "m4v" etc. Those are the two formats I will be using in my project. It works like this: if I’m asking for a WMV file and the enclosure is of that type, it writes the enclosure line. The same for M4V. If there is no format specified both enclosure tags are written. If the format isn’t recognized, no enclosure tag is written.

Now I can construct a URL like this and get a feed for the specified file format:

http://www.domain.com/wp_dir/index.php?feed=rss2&format=m4v

That’s all fine and good but I wanted nicer URLs. A little bit of work in my .htaccess file:

RewriteRule ^wp/feed/?(.*$) /wp/index.php?feed=rss2&format=$1 [QSA,L]

And now a URL like this works:

http://www.domain.com/wp_dir/feed/m4v

+++

This was a quick and dirty hack for my specific purposes. There’s definitely smarter and more extensible ways to do this. I wish I had time to make a plugin or something to help out more people, but this is what I have presently.

Sorry for the funky formatting of some of the code above. You’ll probably need to cut and paste into your text editor to make any sense out of it. permanent link to this post

MTAA-RR » news » twhid » geek » create 2 feeds for different media formats in wordpress 1 5 quick and dirty


1997 - 2006 M.River & T.Whid Art Associates. Some Rights Reserved. MTEWW.com is licensed under a Creative Commons License with the exception of Website Unseen titles which are covered by agreements with individual collectors and otherwise where noted.