WordPress : Replace string in content of all pages
Don’t want to open the wordpress post editor for individual posts to change text in order to replace a specific string that is found in all posts or pages of your website, then this method is far better than doing a Search replace on your mysql db for a given text. You will learn to :-
- Replace text string if found in content of all posts and pages.(including all custom post types)
- Replace text string if found in content of all posts and pages only (exclude custom post types)
- Replace text String if found in a single specific post or page.
- Replace text string in all pages only (excluding posts).
- Replace text string in all posts only (excluding all other custom post types and pages).
1. Replace text string if found in content of all posts and pages (including all custom post types)
In order to do this we will add a function to our theme’s functions.php file or to the child theme’s functions.php file and hook it to the wordpress add_filter to the_content(). The sample code is as under :-
add_filter('the_content', 'filter_my_post'); function filter_my_post($content){ if ( is_singular() && in_the_loop() && is_main_query() ) { $string = 'ADD STRING / URL TO BE REPLACED'; //your string to be replaced $replace = 'ADD THE NEW CONENT HERE'; $content = str_replace( $string, $replace, $content ); } return $content; }
Replace the contents of the $string and $replace variables as per your requirement. The above code will apply to all posts and pages ( also custom post types) at runtime, The settings will not be saved to the database, however, the text string will be replaced before the page is rendered.
This applies to only the content of the single post and to the content of a single page and not to sidebar and footer.
2. Replace text string if found in content of all posts and pages only (exclude custom post types)
If you would like to confine the replacement of a string to posts and pages only excuding the custom post types then replace line 3 of the code in Method 1 as
if ( (is_singular('page') || is_singular('post')) && in_the_loop() && is_main_query() ) {
3. Replace text String if found in a single specific post or a page
This method of replacing is the same as Method 1, however, we will filter only the specific post or page by its ID and the changes will apply only to that post or page.
To get the post ID, go to your post editor in WordPress admin area by Selecting Posts -> All Posts. Hover the mouse over the title of the Post you want to replace the text string for. Observe the link that appears on the bottom left, above the task bar. See the portion of the link containing
/post.php?post=1234&…. Here the number after post= is the ID of the post.
To get the page ID, go to your post editor in WordPress admin area by Selecting Posts -> All Pages.Rest action is same as that for getting the post ID.Pages are also like posts having a post ID in wordpress.
Now add the following code to the functions.php file of your parent/child theme.
add_filter('the_content', 'filter_post_1254'); function filter_post_1254($content){ if ( is_singular() && in_the_loop() && is_main_query() ) { global $post; if ($post->ID == 1254){ // 1254 is post/page ID you have found $string = 'ADD STRING / URL TO BE REPLACED'; //your string to be replaced $replace = 'ADD THE NEW CONENT HERE'; $content = str_replace( $string, $replace, $content ); } } return $content; }
4. Replace text string in all pages only
If you would like to confine the replacement of a string to all pages only then replace line 3 of the code in Method 1 as
if ( is_singular('page') && in_the_loop() && is_main_query() ) {
5. Replace text string in all posts only (excluding all other custom post types and pages).
If you would like to confine the replacement of a string to all pages only then replace line 3 of the code in Method 1 as
if ( is_singular('post') && in_the_loop() && is_main_query() ) {
That is all.
If you liked this article please do leave a reply and share it with friends.
Thanks.