Add Snippet To Project
Have you ever wished you could easily find and replace specific keywords within your WordPress site’s content and excerpts? Maybe you’ve re-branded or changed product names and need to ensure consistency across your site. Fortunately, there’s a method to this – with just a bit of code, you can set up a function to perform this task for you automatically. In this article, we’ll guide you through the process of replacing keywords in the_content and the_excerpt in WordPress. So, grab your favorite beverage, settle in, and let’s dive into some WordPress coding.
function wpturbo_replace_keywords_content_excerpt($content){
$keywords_to_replace = array('Keyword1', 'Keyword2');
$replacement_keywords = array('Replacement1', 'Replacement2');
$content = str_replace($keywords_to_replace, $replacement_keywords, $content);
return $content;
}
add_filter('the_content', 'wpturbo_replace_keywords_content_excerpt');
add_filter('the_excerpt', 'wpturbo_replace_keywords_content_excerpt');
This code snippet defines a function wpturbo_replace_keywords_content_excerpt
that operates on $content
passed to it when it’s called. This function deals with replacing certain keywords in the content and excerpt of the WordPress posts. The parameters to this function include both the old keywords that need to be replaced and the new keywords that will replace the old ones.
$keywords_to_replace = array('Keyword1', 'Keyword2');
$replacement_keywords = array('Replacement1', 'Replacement2');
The first line of the function is declaring an array $keywords_to_replace with two elements ‘Keyword1’ and ‘Keyword2’. These are placeholders for the words which are to be replaced within the content or excerpt of a WordPress post.
The second line is declaring an array called $replacement_keywords with two elements ‘Replacement1’ and ‘Replacement2’. These values will replace ‘Keyword1’ and ‘Keyword2’ respectively in the content.
Next, we have the str_replace() function:
$content = str_replace($keywords_to_replace, $replacement_keywords, $content);
The str_replace function is a built-in PHP function that takes three parameters— the array of values to be replaced, the array of replacements and the subject data (in this case $content) to search for replacements.
The $content
string is returned after all instances of the keywords have been replaced:
return $content;
In the final lines of this snippet, WordPress filters are used to apply the newly created function wpturbo_replace_keywords_content_excerpt()
to the content and excerpt of WordPress posts:
add_filter('the_content', 'wpturbo_replace_keywords_content_excerpt');
add_filter('the_excerpt', 'wpturbo_replace_keywords_content_excerpt');
What these lines do is that whenever ‘the_content’ or ‘the_excerpt’ is called in WordPress, the wpturbo_replace_keywords_content_excerpt
will be applied to the content or excerpt returned by it. These hooks thereby ensure that the replacement function is applied across all instances of ‘the_content’ and ‘the_excerpt’ in the WordPress posts.