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.
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. :(