Automatically create links from keywords in all articles

Linking words or entire phrases in WordPress couldn’t be easier. While writing an article, all you have to do is select a word/phrase and click on the link button where you can paste link destination. If you know basic HTML, you can also create links directly from Text editor by writing your own code if necessary.

WordPress links

While linking word by word is easy and can be quite fun if you’re slowly working on a new post, what if you wanted to link a specific word/phrase throughout your entire posts/pages collection?

If you’ve just started with your WP powered website and have only a few articles to deal with, it wouldn’t be a problem linking them all manually. But what if you have decided to link a word in all articles and you have hundreds of them?

Don’t worry; in this article, we are about to show you a function which will save your time.

Create links from specific words:

Whether you want to link specific website name to its own URL or you have started your referral program and want to link every computer related words to your referral website which sells computer equipment, this function will help you insert a link tag within the word/phrase of choice.

  1. Open functions.php
  2. Copy and paste the following function:
  3. function link_words( $text ) {
    $replace = array(
    'google' => '<a href="http://www.google.com">Google</a>',
    'computer' => '<a href="http://www.myreferral.com">computer</a>',
    'keyboard' => '<a href="http://www.myreferral.com/keyboard">buy keyboard here</a>'
    );
    $text = str_replace( array_keys($replace), $replace, $text );
    return $text;
    }
    add_filter( 'the_content', 'link_words' );
    add_filter( 'the_excerpt', 'link_words' );
    
  4. Change keywords and links in array
  5. Save changes

As you can see from examples in the code, it is possible to replace any word or any phrase found in your articles. Also, it is possible to replace the word/phrase itself and change the link entirely.

While you can simply add the link to word “computer”, you could add the link and change the word “computer” into “computer system” or anything you like.

While this function will help you catch up all those left behind articles, it is pretty much basic as it gets. If you want more control over links, especially if you’re working with referrals, it would be a smarter choice to go with one of the specialized plugins.

Since you’re already working with links, you might be interested in instructing WordPress on opening all external links in new tabs or maybe even linking posts and pages to an external URL.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “Automatically create links from keywords in all articles

  1. […] matters Internal Linking is used to help all the search engines understand your website better. You need to linking to different posts and pages in every post you publish on your blog. It’s something that has a considerable impact on the SEO […]

Leave a Reply

Your email address will not be published. Required fields are marked *