Home / functions.php / Link keywords or replace text in all blog posts
  • How to Link keywords or replace text in all blog posts

    link-keywords-or-replace-text-in-all-blog-posts

    Adding this to the functions.php of your wordpress theme will let you replace text within your blog post. This is a great option for linking multiple pages in your blog with keywords. 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: Extensive use of this method may cause performance issues, use sparingly.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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');
    

    ( WordPress codex functions, hooks, in this snippet. )

    the_content, add_filter, wp,
  • Guest

    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.