First of all create a function that use get_shortcode_regex
to get the post_content of a post with the shortcode replaced.
Note that any other shoertcode will be untouched
function get_replaced_sourcecode_sc( $post ) {
if ( empty($post) ) global $post;
if ( empty($post) || ! isset($post->post_content) ) return false;
$content = $post->post_content;
if (
preg_match_all( "https://wordpress.stackexchange.com/". get_shortcode_regex() .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches ) && in_array( 'sourcecode', $matches[2] )
) {
foreach ( $matches[2] as $i => $sc ) {
if ( $sc == 'sourcecode' )
$now = $matches[0][$i];
$replace = do_shortcode($now);
$content = str_replace( $now, $replace, $content );
}
}
return $content;
}
Now in your functions.php
you can also add following code:
function replaced_sourcecode_sc_all() {
$already = get_transient('replaced_sourcecode') ? : array();
$all = get_posts('nopaging=1post_type=post');
if ( ! empty($all) ) {
if ( count($all) == count($already) ) {
error_log('replaced_sourcecode_sc_all already done its work');
return;
}
$done = array();
foreach ( $all as $one ) {
if ( in_array($one->ID, $already) ) continue;
$content = get_replaced_sourcecode_sc( $post );
$post = array('ID' => $one->ID, 'post_content' => $content);
$do = wp_update_post( $post );
if ( $do ) $done[] = $do;
}
set_transient('replaced_sourcecode', $done);
}
}
add_action('shutdown', 'replaced_sourcecode_sc_all');
Now, enable WP logging, and then visit your site 2 or 3 times. Check the error log and if you find the string “replaced_sourcecode_sc_all already done its works” you can remove the function (or simply comment out the ‘add_action’ line).