Created
November 24, 2018 11:45
-
-
Save osbulbul/ce5f7dcb18c3f851dc1ca5b6978cb4fc to your computer and use it in GitHub Desktop.
This is plugin that helps you to search and replace any post content. Put this file to wp-content/mu-plugins folder and edit as you wish. Refresh the page and delete file after you see "success" on page.
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 | |
/* | |
Plugin Name: Posts Search Replace | |
Description: This plugin helps you to search and replace any post content. | |
Version: 0.1.0 | |
Author: Oz | |
*/ | |
$post_type = "page"; //use page or post or any post type you created | |
$filter_type = "include"; //use exclude or include | |
$filter_array = array(); //add post ids | |
$search = "Find this text"; //enter what you want to search. | |
$replace = "Replace with this"; //enter what you want to replace. | |
function execute_search_replace(){ | |
global $post_type, $filter_type, $filter_array, $search, $replace; | |
$args = array( | |
'post_type' => $post_type, | |
'posts_per_page' => -1, | |
); | |
if($filter_type == "exclude"){ | |
$args["post__not_in"] = $filter_array; | |
} elseif ($filter_type == "include") { | |
$args["post__in"] = $filter_array; | |
} | |
$srquery = new WP_Query($args); | |
foreach ($srquery->get_posts() as $post) { | |
$new_post_content = str_replace($search, $replace, $post->post_content); | |
$post->post_content = $new_post_content; | |
wp_update_post($post); | |
} | |
die(var_dump("success")); | |
} | |
add_action("init", function(){ | |
execute_search_replace(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment