Forums

Auto link specific word in post. (4 posts)

  1. spunkyw
    Member
    Posted 2 years ago #

    Hi, I'm looking for a plugin that can automatically link a word in a post.

    example; i set URL "google.com" for word "google"
    so every time I write "google" in my post it automatically link the word to "google.com"

    any suggestion of plugins? I tried searching.

    thanks for reading. =)

  2. takien
    Member
    Posted 2 years ago #

    You can add this function to functions.php in your theme directory (create if you don't have)

    function my_google_link($content) {
    $content = str_replace('Google','<a href="http://www.google.com">Google</a>',$content);
    return $content;
    }
    
    add_filter('the_content','my_google_link');
  3. spunkyw
    Member
    Posted 2 years ago #

    thanks, but that method doesn't seem easy because I have infinite words and different links on each words..any other way?

  4. takien
    Member
    Posted 2 years ago #

    You need to list your words and links in an array and do str_replace in foreach.

    function replace_infinite_words($content) {
    
    $wordlists = Array('word1' => 'http://example.com/1',
    		'word2' => 'http://example.com/2',
    		'word3' => 'http://example.com/3',
    		);
    
    foreach($wordlists as $key=>$value) {
    	$content = str_replace($key,'<a href="'.$value.'">'.$key.'</a>',$content);
    }
    
    return $content;
    }
    
    add_filter('the_content','replace_infinite_words');

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.