Skip to content

Instantly share code, notes, and snippets.

@waylay
Created July 4, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylay/7c49d3125fb07522c2201c7406556c94 to your computer and use it in GitHub Desktop.
Save waylay/7c49d3125fb07522c2201c7406556c94 to your computer and use it in GitHub Desktop.
Replace shortcode with its output in all posts
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( '/'. get_shortcode_regex() .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches ) && in_array( 'purehtml', $matches[2] )
) {
foreach ( $matches[2] as $i => $sc ) {
if ( $sc == 'purehtml' )
$now = $matches[0][$i];
$replace = do_shortcode($now);
$content = str_replace( $now, $replace, $content );
}
}
return $content;
}
function replaced_sourcecode_sc_all() {
$all = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' ));
if ( ! empty($all) ) {
global $wpdb;
foreach ( $all as $one ) {
$content = get_replaced_sourcecode_sc( $one );
if(strcmp($content, $one->post_content) !== 0){
$query = "UPDATE ".$wpdb->prefix."posts SET post_content='".$content."' WHERE ID = '".$one->ID."'";
$wpdb->query($query);
}
}
}
}
add_action('shutdown', 'replaced_sourcecode_sc_all');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment