0

We're using podpress as our podcast player, but we'd like to give the option to download the file on a page without a player. So I'm trying to figure out how to extract the URI from the metadata.

I'm new to PHP, and struggling to learn it, so I've gotten this far:

<?php  
    $custom_fields = get_post_custom(get_the_ID());
    $my_custom_field = $custom_fields['_podPressMedia'];
    foreach ( $my_custom_field as $key => $value ){
        echo $key . " => " . $value . "<br />";
    }
?>

which outputs

0 => a:1:{i:0;a:10:{s:3:"URI";s:85:"http://www.writingexcuses.com/wp-content/uploads/Writing_Excuses_8_1_Microcasting.mp3";s:5:"title";s:33:"Writing Excuses 8.1: Microcasting";s:4:"type";s:9:"audio_mp3";s:4:"size";i:12759440;s:8:"duration";s:5:"17:43";s:12:"previewImage";s:86:"http://legacieslost.com/writing/wp-content/plugins/podpress/images/vpreview_center.png";s:10:"dimensionW";i:0;s:10:"dimensionH";i:0;s:3:"rss";s:2:"on";s:4:"atom";s:2:"on";}}

So I can see the data I want right there, and I know I need to create another function that takes the first result and extracts just the value with the key URI, but I can't figure out what that function should look like. I haven't really figured out "array thinking" yet, despite a lot of time looking at this page on the PHP manual.

http://php.net/manual/en/control-structures.foreach.php

Any advice on construction?

1
$meta = get_post_meta(get_the_ID(), '_podPressMedia', true);
$meta = maybe_unserialize($meta);
if (is_array($meta) && count($meta) > 0)
    foreach($meta as $item){
        //do something...
    }

Please, do not use unserialize/serialize with php data stored structures there is native maybe_serialize/maybe_unserialize. Retriving single value from postmeta table also doesn't require get_post_custom, all you need to do to use get_post_meta whish will return only required meta key data assinged to the post.

0

EDIT: using the get_post_meta() returns an unserialized array, re: post from Oleg Butuzov. Thanks! -- I updated the code examples.

The output of the array is serialized and must be unserialized before you can access the information with php.

After some help from this post I was able to use unserialize(); to access the uri.

  <?php 
      //grabs the array with the meta fields of podPress for a particular post or page.
      $pp_array = get_post_meta(get_the_ID(), '_podPressMedia', true);

      $pp_array = $pp_array[0];
  ?>

  <a href="<?php echo esc_attr($pp_array["URI"]) ?>" title="<?php echo esc_attr($pp_array["title"]);?>.mp3">Download</a>

For those who are curious, here is the result of $pp_array.

Step 1:

$pp_array = get_post_meta(get_the_ID(), '_podPressMedia', true);
var_dump($pp_array);


RETURNS

array(1) { [0]=> array(10) { ["URI"]=> string(85) "http://www.writingexcuses.com/wp-content/uploads/Writing_Excuses_8_1_Microcasting.mp3" ["title"]=> string(33) "Writing Excuses 8.1: Microcasting" ["type"]=> string(9) "audio_mp3" ["size"]=> int(12759440) ["duration"]=> string(5) "17:43" ["previewImage"]=> string(86) "http://legacieslost.com/writing/wp-content/plugins/podpress/images/vpreview_center.png" ["dimensionW"]=> int(0) ["dimensionH"]=> int(0) ["rss"]=> string(2) "on" ["atom"]=> string(2) "on" } } - See more at: http://www.writingexcuses.com/season-archives/izzy-detective/#sthash.jr9uR8xx.dpuf

Step 2:

$pp_array = $pp_array[0];
var_dump($pp_array);

RETURNS

array(10) { ["URI"]=> string(85) "http://www.writingexcuses.com/wp-content/uploads/Writing_Excuses_8_1_Microcasting.mp3" ["title"]=> string(33) "Writing Excuses 8.1: Microcasting" ["type"]=> string(9) "audio_mp3" ["size"]=> int(12759440) ["duration"]=> string(5) "17:43" ["previewImage"]=> string(86) "http://legacieslost.com/writing/wp-content/plugins/podpress/images/vpreview_center.png" ["dimensionW"]=> int(0) ["dimensionH"]=> int(0) ["rss"]=> string(2) "on" ["atom"]=> string(2) "on" } - See more at: http://www.writingexcuses.com/season-archives/izzy-detective/#sthash.iiQ43LEp.dpuf 

Step 3:

<p><?php echo $pp_array["URI"];?></p>

RETURNS

http://www.writingexcuses.com/wp-content/uploads/Writing_Excuses_8_1_Microcasting.mp3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.