I ran into an interesting problem today. My podcasting plugin over on my blog flaked out on an upgrade. The music player still works, but the download links disappeared. Now that site relies on the music downloads. I just couldn’t have that. The developers are looking for a fix, but I needed the functionality now. So I came up with a nifty band-aid.
The first thing I did was look at how the music was included into my post. I fill out a little form on the backend and the music gets inserted into my posts, with a player, and links and everything. Well I noticed that this was all done with a custom field. Once I entered my music information and saved the post, a custom field was created called enclosure. This enclosure has enclosed in the value all the information to make everything work. My gut instinct was to just echo out
get_post_meta($post_id, $key, $single);
inside of an a href link to make it work. But I had a problem. The value was actually set up like this:
url to media
size of file
type of file
encoding info
We had multiple lines of data. I originally thought that if I set the $single value to true, it would only echo out the first line. You can read about the $single value, and all about custom fields here. Well, that isn’t the case. The $single set to true only grabs the first value if you have multiple key value pairs with the same key. I had a multi-line value, and only needed the first line, the url to the media.
So I did a fair bit of googling, and came across this article. This article showed me that if I have a multi-line value set up, I can include
$items = get_post_meta($post->ID, 'ItemInfo');
in my loop to grab the entire value.
It would then return that all in an array, which I could break up in a foreach loop, breaking apart the multi-line value by enforcing the line breaks that are in the custom field value. The example code given on that site looks like:
foreach($items as $item) { $item = nl2br($item); list($name,$src,$price,$desc) = explode('<br />',$item); echo "<div class="item">"; echo "<h2>$name</h2>"; echo "<img src="".trim($src)."" />"; echo "<br />Price: $price"; echo "<br />Description: $desc"; echo "</div><br />"; }
If you look at the link I provided, it will make a lot more sense, I’m just summarizing here. Now I didn’t need all the info output. I just needed the first line output (the media url) as a download link. So I took that example code given, and tweaked it for my own use. Here’s what I came up with real quick:
<?php if( get_post_meta($post->ID, "enclosure", true) ): ?> <?php $items = get_post_meta($post->ID, 'enclosure'); ?> <?php foreach($items as $item) { $item = nl2br($item); list($name,$src,$price,$desc) = explode('<br />',$item); echo "<a href="$name">Download</a>"; } ?> <?php endif; ?>
And it worked perfectly. We set up on line 1 to only do the code if the enclosure key actually exists. Then on line two we grab the values associated with enclosure key, and spit it out in an array. We break each line down on a line break and assign it to a variable. And then I only needed the first line, which is assigned to $name, so I just spit out $name inside of a link.
I know this will come in handy in the future for me. So I just wanted to share the info with you in case you run into a similar situation. My downloads work again until the plugin is fixed, and I’m happy!