Are you looking for a way to replace text within your blog post? This is a great option for linking multiple pages in your blog with keywords. While there’s probably a plugin for this, we have created a quick code snippet that you can use to link keywords or replace text in all blog posts.
Note: Extensive use of this method may cause performance issues, use sparingly.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function replace_text_wps($text) { $text = str_replace('TEXT_TO_REPLACE', 'TEXT_YOU_WANT_TO_REPLACE_WITH', $text); $text = str_replace('tech support', '<a href="/techsupport">Tech support</a>', $text); $text = str_replace('computers', '<a href="/computers">Computers</a>', $text); return $text; } add_filter('the_content', 'replace_text_wps');
Don’t forget to change the “TEXT_TO_REPLACE” and the “TEXT_YOU_WANT_TO_REPLACE_WITH” lines in the above code add new lines for more items.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress accordion plugins and how to set up author tracking in WordPress with Google Analytics.
Is this likely to impact speed of the website?
This method would likely impact performance if you replaced a lot of items. However if you used a method like this along with wp-cache or super-cache or many other cache plugins. They would cache Worpress pages in a static file for serving future requests directly from the file rather than loading and compiling the whole PHP code so this would help.
This is just a nice simple method to replace a few items, I will place a note that excessive use of this could cause performance issues.