Skip to content

Instantly share code, notes, and snippets.

@mrsize
Created December 6, 2018 16:12
Show Gist options
  • Save mrsize/cb43f519c13b73b2577153d4de9c1ff0 to your computer and use it in GitHub Desktop.
Save mrsize/cb43f519c13b73b2577153d4de9c1ff0 to your computer and use it in GitHub Desktop.
WordPress Search & Replace in posts
<?php
/**
*
* Search & Replace in WordPress posts
*
*/
function mytheme_go_replace(){
// sidebar :
mytheme_replace_content(
$my_postid = 12,
$toreplace = "REPLACEME",
$replabeby = "REPLACED"
);
// footer :
mytheme_replace_content(
$postid = 22,
$toreplace = "REPLACEME",
$replabeby = "REPLACED"
);
}
function mytheme_replace_content($postid, $toreplace, $replabeby){
$content = get_post_field('post_content', $postid);
$newcontent = str_replace($toreplace, $replabeby,$content);
$blogname = get_bloginfo('name');
if (strpos($content, $toreplace) !== false) {
$mypost = array(
'ID' => $postid,
'post_content' => $newcontent
);
wp_update_post( $mypost );
}
}
// add_action('wp_head', 'mytheme_go_replace');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment