Sometime you want to link some words inside your posts and page to other URLs. For example if you want to link WordPress to one of your page describing WordPress or you want it directly to be linked with WordPress.Org. In this article we will show you how you can do it in your WordPress based website.
Open your theme’s function.php file and add the following code replacing words and URLs with your own.
function replace_text_wps($text){
$replace = array(
// ‘KEYWORD TO REPLACE’ => ‘URLs TO REPLACE WITH’
‘wordpress’ => ‘<a href=”http://wordpress.org”>wordpress</a>’,
‘buddypress’ => ‘<a href=”http://buddypress.org”>buddypress</a>’,
‘sangkrit’ => ‘<a href=”https://sangkrit.net”>SANGKRIT</a>’
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter(‘the_content’, ‘replace_text_wps’);
add_filter(‘the_excerpt’, ‘replace_text_wps’);
In the above given example ‘wordpress’ is liked with wordpress.org, buddypress is linked with buddypress.org and sangkrit is linked with sangkrit.net. One more thing, it can also replace words with different styles like in the third keyword sangkrit is linked with sangkrit.net and it it also replaced with uppercase characters which means wherever this script would be finding sangkrit it would be replacing it with uppercase SANGKRIT linked to sangkrit.net.
If you are running a WordPress Multisite install then you can force this hack across your multisite networks. You can create a simple plugin. For creating a plugin first of all create a PHP file may be keywords.php etc and add everything as shown here:
<?php
/*
Plugin Name: Link Keywords
Plugin URI: http://lamp.sangkrit.net
Description: Automatically link keywords
Author: Shardul Pandey
Version: 1.0
Author URI: http://lamp.sangkrit.net
*/function replace_text_wps($text){
$replace = array(
// ‘KEYWORD TO REPLACE’ => ‘URLs TO REPLACE WITH’
‘wordpress’ => ‘<a href=”http://wordpress.org”>wordpress</a>’,
‘buddypress’ => ‘<a href=”http://buddypress.org”>buddypress</a>’,
‘sangkrit’ => ‘<a href=”https://sangkrit.net”>SANGKRIT</a>’
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter(‘the_content’, ‘replace_text_wps’);
add_filter(‘the_excerpt’, ‘replace_text_wps’);?>
Now upload this file to your plugin’s folder present in your website’s wp-content directory and Network Activate it from plugins section of network admin dashboard. That’s all, your keywords would be getting automatically linked with your URLs in all websites of your multisite network.