WordPress.org

Ready to get started?Download WordPress

Forums

using wordpress rss to generate xspf playlist! (8 posts)

  1. ericmcgregor
    Member
    Posted 7 years ago #

    Hi, I am really interested in adapting the xspf music player ( http://musicplayer.sourceforge.net/ ) into a custom podcasting plugin. I have looked at, literally a hundred different approaches, and am not entirely happy with how complicated some are.

    Right now my goal is this: to use the ability of wordpress to detect .mp3 enclosures to dynamically build a xspf playlist file for use with the music player.

    I'm not a great programmer, but I know a little. So how does wordpress know that if you link to an mp3 file, to build it as an enclosure in the rss feed?

    Thanks a lot!

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    So how does wordpress know that if you link to an mp3 file, to build it as an enclosure in the rss feed?

    This actually happens as part of the execute pings process. Basically, it attempts to get the file from whatever server it's on, even if it's local. Actually, it doesn't get the whole file, just the server headers for it. Then it looks at the Content-Type returned by the server. If that Content-Type: value contains "audio" or "video", then it knows it's a media file and creates an "enclosure" metadata value for the file in the post.

    When the RSS is generated, it just looks for that "enclosure" metadata and creates an enclosure tag accordingly.

    So if you want to generate something using that, just look for an enclosure key in the metadata. As you loop through the posts, do something like this:

    $custom_fields = get_post_custom();
    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 );
    // your output here instead of the following
    echo "url=".trim( htmlspecialchars($enclosure[ 0 ]) );
    echo "length=".trim( $enclosure[ 1 ] );
    echo "type=".trim( $enclosure[ 2 ] );
    } } } } }

  3. ericmcgregor
    Member
    Posted 7 years ago #

    Wow, this looks really promising! I'm going to really look at this tomorrow and let you know how it works out.

    I plan on making a seperate .php file that will build a valid .xspf playist document based on info from the loop. I'm then going to hook the xspf web music player into that play list.

  4. ericmcgregor
    Member
    Posted 7 years ago #

    Ok. I'm having some trouble getting this to work. I've made a standalone .php file with the following:


    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');

    $custom_fields = get_post_custom();
    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 );

    // your output here instead of the following

    echo "hello";
    echo "url=".trim( htmlspecialchars($enclosure[ 0 ]) );
    echo "length=".trim( $enclosure[ 1 ] );
    echo "type=".trim( $enclosure[ 2 ] );

    }
    }
    }
    }
    }

    ?>

    I'm not getting anything returned on the page. Do I need to run this inside the loop? Should I actively try and query a post when I access it (playlist.php?p=8)?

    I'm trying to build the address of the enclosure into a xspf type playlist. Thanks!

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Do I need to run this inside the loop?

    Yes, you do. Look more closely at wp-rss2.php, and do sorta like it does.

  6. ericmcgregor
    Member
    Posted 7 years ago #

    huh. I am really missing something. I'm sorry, I'm not a very good programmer at all. It's really frustrating because conceptually i get how it should work, but then I can't make it happen.

    I put the above code in playlist.php file with a call to the wp-blog-header. I started the loop and am accessing the file like this: playlist.php?p=8

    Nothing happens. I looked at the rss2 file... here is where i think the problem is:

    I'm not specifically putting this file in a custom field as an eclosure. I'm relying on wordpress to autodetect the LINK in the post and then do all of it's magic. so I am not manually storing this as an eclosure. I'm trying to make it as simple as possible, and I'm using the upload -> send to editor fuction to put my podcast in the post.

    Does that make sense? am I approaching this completely wrong.

  7. ericmcgregor
    Member
    Posted 7 years ago #

    Ok! you know what, it is working. However, now it's repeating the address to the enclosure several times. :( I'll keep diggin. but real progress! Thanks!

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    I honestly cannot think of how to make this any more plain. I'd like to help, but I'm not understanding where the problem is. Custom fields are fairly straightforward and easy to do, but I'm not exactly sure what you want out of this script. Do you want links from one post? Many posts? What's the goal here? What are you going from and what are you going to?

    For custom fields, look at this simple example:

    <?php
    if (empty($wp)) {
    require_once('wp-config.php');
    wp();
    }
    header('Content-type: text/plain; charset=' . get_settings('blog_charset'), true);
    if ($posts) {
    foreach ($posts as $post) {
    start_wp();
    $custom_fields = get_post_custom();
    var_dump($custom_fields);
    }
    }
    ?>

    Put that in a file like "test.php" and run it in a browser. It dumps all your custom fields for however many posts you have configured to show up on your front page. It just dumps text, of course, but you get the idea. This is just the most basic example, you could use a different kind of loop to get different amounts of posts or what have you.

Topic Closed

This topic has been closed to new replies.

About this Topic