Created
December 6, 2018 16:12
-
-
Save mrsize/cb43f519c13b73b2577153d4de9c1ff0 to your computer and use it in GitHub Desktop.
WordPress Search & Replace in posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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