0

I have a lyrics website on WordPress with thousands of lyrics, and I am looking to embed a player on each one. But I have realised it would be tedious and time consuming to do that.

How would I display a dynamic audio player that would find and play a track (using the current post title) from soundcloud?

This could preferably be a html5 audio player or an actual soundcloud embed.

The way it plays out in my head is to do a search, then pass the first result (track url) into a variable, then call the variable in the src part of the player.

  • What does you currently title / url structure look like? A soundcloud link consists of two parts {author}/{track} - soundcloud.com/djmambo/… so you might need more than just the title. – jgraup Jan 5 '16 at 21:30
  • Hi @jgraup, I have noticed that soundcloud uses the track ID for embeds. Here is an example: <iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/216846955&amp;color=00aabb"></iframe> – Stanley Umeanozie Jan 5 '16 at 21:53
  • It just seems like whatever you do, pulling an ID or username from title seems like it could easily break. – jgraup Jan 5 '16 at 21:58
1

If a shortcode isn't too much effort you can use it to construct the soundcloud url for embedding.

add_shortcode('soundcloud_auto', 'soundcloud_auto_shortcode');

function soundcloud_auto_shortcode($atts) {
    global $post;

    $title = str_replace(" ", "", $post->post_title);
    $title = str_replace("-", "", $title);
    $title = str_replace("_", "", $title);

    $slug = sanitize_title($title, str_replace("-", "", $post->post_name));

    return wp_oembed_get(esc_url("http://soundcloud.com/$slug"));
}

Or do something similar using the_content hook.

add_filter('the_content', 'add_soundcloud');

function add_soundcloud($content){
    global $post;

    $title = str_replace(" ", "", $post->post_title);
    $title = str_replace("-", "", $title);
    $title = str_replace("_", "", $title);

    $slug = sanitize_title($title, str_replace("-", "", $post->post_name));

    return $content . wp_oembed_get(esc_url("http://soundcloud.com/$slug"));
}

The API might be a cleaner route.

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.