Auto-post wp-itunes info

So, after fiddling with the previously mentioned wp-itunes WordPress plugin, I have got it automatically adding the last song played when I publish — no more of this typing crap. Hit more to get the code changes…

Here is the code that I added, complete with ugly debugging code!:

add_action('publish_post', 'add_meta_tunes', 7);
function return_wp_itunes($limit = 10) {
        global $wpdb, $table_itunes, $blank_image, $local_offset;

        $results = $wpdb->get_results("SELECT * FROM $table_itunes ORDER BY added_date DESC LIMIT $limit");
        $retstring = "";
        if ($results) {
                foreach ($results as $itune) {
                        if ($itune->image == "")
                                $itune->image = $blank_image;

                        $retstring .= "<div class="itune">n";
                        $retstring .= "$itune->artist - $itune->album - $itune->trackn";
                        $retstring .= "</div>n";
                }
        }
        return $retstring;
}

function add_meta_tunes($post_ID) {
        global $wpdb, $tablepostmeta;
        global $debug;

        if( !isset($post_ID)) $post_ID = $_REQUEST['post_ID'];

        $metakey       = $wpdb->escape("tunes");
        $metavalue     = $wpdb->escape(return_wp_itunes(1));
        $log = fopen("/tmp/wpitunes.log", "w");
        fwrite($log, "Just testingn");
        // Quick-n-dirty check for dups:
        $dupcheck = $wpdb->get_results("SELECT post_id FROM $tablepostmeta WHERE post_id='$post_ID'",ARRAY_A);
        if ($dupcheck[0]['post_id']) {
                $id = $dupcheck[0]['post_id'];
                fwrite($log, "Skipping duplicate post ID = $idn");
                fclose($log);
                return $post_ID;
        }

        fwrite($log, "name = $metavaluen");
        fclose($log);

        if (!empty($metavalue)) {

                $result = $wpdb->query("
                                INSERT INTO $tablepostmeta
                                (post_id,meta_key,meta_value)
                                VALUES ('$post_ID','$metakey','$metavalue')
                        ");
        }
        return $post_ID;
}
<pre>

Related Posts with Thumbnails
Share

One Comment

  1. claire says:

    Hi

    I came accross this page whilst trying to find a way to insert the currently playing song into each WordPress post when I publish. I’ve read back on the thread but am a bit confused about whether this is what I need!

    I have got the wp-itunes plugin working so that the last song played shows in the sidebar of my blog, but I really want at the end of each post. I THINK that’s what you describe above but I’m not sure what file I should insert your code in?

    If you have time it would be great if you could let me know – thanks :-)

  2. Nic says:

    Ok — so the change I made was to “wp-content/themes/GreenTrack/post.php” — obviously you will need to change this for whatever theme you are using. I use the custom field plugin that pulls the value of the itunes item that gets added with each post.

    I added this before the ‘post-content’ block:
    <small> <?php echo c2c_get_custom(‘tunes’, ‘iTunes says:’); ?></small>

Leave a Reply