0

I'm working on a blog that has over 4000 posts. Most of them have a soundcloud embed iframe in the post.

I am creating a new custom field (using Advanced Custom Fields) that uses a custom player using soundclouds API...and i just need the URL of the soundcloud track in a custom field.. NOT the embed info.

Is it possible to scan the blog and retrieve the info from the soundcloud embed and put that into a custom field?

I used Display Embedded Videos by D.Biot, a plugin, that basically did the same thing for youtube.

Any help would be much appreciated! If you need me to post any code, let me know but i didn't think it was relevant...

----- SOUNDCLOUD GENERIC EMBED------ Here is just a random embed from soundcloud.

<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/212039125&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
2
  • Everything is Possible [full stop]. You need to know the embed code's pattern first. Then using a SQL Query with LIKE '%string%' you can grab the content. For critical embed code you might need Regular Expression. Grabbing the right content and then you need a simple insert SQL or WordPress' update_post_meta() to insert that information into that Custom Field. But it's too broad to discuss here. :( Commented Sep 29, 2015 at 7:36
  • ahh, i don't know, but thank you @MayeenulIslam - i think this is a little over my head. i've added the soundcloud embed ot the post, if you can help at all clarifying it would be incredible! Commented Sep 29, 2015 at 8:12

1 Answer 1

2

It's a rudimentary plugin, not tested. It will function once, when the plugin is activated.

  • Copy the code
  • Make it a file named __once.php
  • Save the file in wp-content\plugins\__once\
  • Browse the wp-admin/plugins.php, and activate the plugin named __once.

WARNING: It's not tested. It's recommended to test first, fix bugs, fix the way it should be for yours, then use it or build your one.

<?php
/**
 * Plugin name: __once
 * Plugin URI: http://wordpress.stackexchange.com/a/204002/22728
 * Description: A onetime minimal plugin to update all the postmeta from the content iframes.
 */


/**
 * Grabbing all the posts and populating postmeta.
 * Once, only on this plugin's activation.
 */
function wpse_do_once() {
    $all_posts = new WP_Query(
        array(
            'post_type'         => 'any',
            'post_status'       => array( 'publish, pending, draft' ),
            'posts_per_page'    => -1
        )
    );
    while ( $all_posts->have_posts() ) : $all_posts->the_post();
        $post_content = get_the_content();

        //the src information from iframe
        $src = wpse_get_iframe_src( $post_content );

        foreach( $src as $link ) {
            //Populating the custom field
            //Assumed: Your custom field id is '_my_embed_link'
            //this will override the values if there's two or more iframes
            if( !empty() ) {
                update_post_meta( get_the_ID(), '_my_embed_link', esc_url( $link ) );
            }
        }


    endwhile;
    wp_reset_postdata();
}
register_activation_hook( __FILE__, 'wpse_do_once' );

/** HELPER FUNCTION */

/**
 * Grab all iframe src from a string.
 * @author Dbranes
 * @link   http://wpquestions.com/question/showChrono/id/10006
 * @param  string $input The content.
 * @return string        The src attribute value.
 */
function wpse_get_iframe_src( $input ) {
    preg_match_all("/<iframe[^>]*src=[\"|']([^'\"]+)[\"|'][^>]*>/i", $input, $output );

    $return = array();

    if( isset( $output[1][0] ) )
        $return = $output[1];

    return $return;
}

How it works:

It's fetching all the posts of any status from the site, then taking all of the contents, and using a custom function (powered with RegEx) (source) fetched the src from the iframes. Then puts the src values to the postmeta (aka Custom Field).

Please note the helper function returns an array of src values if there's multiple in a post content. So a single update_post_meta() will override one with the other. In that case you can choose avoiding the foreach loop and save the array in a serialized form. Then when showing the field data using get_post_meta() you will need to show the individual URL from there.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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