How to add a rel=”nofollow” attribute to all links in WordPress posts

To add a rel=”nofollow” attribute to all links in your WordPress posts, you can follow these steps:

Edit your theme’s functions.php file:

To add the “nofollow” attribute to all links in your posts, you can edit your theme’s functions.php file. Follow these steps:

  1. Log in to your WordPress admin dashboard.
  2. Go to “Appearance” > “Theme Editor.”
  3. In the right sidebar, locate and click on the “functions.php” file.
  4. Add the following code snippet at the end of the file:
function remove_nofollow_from_internal_links($content) {
$site_url = get_site_url();
$pattern = "/<a(.*?)href=[\"']" . preg_quote($site_url, "/") . "(.*?)[\"'](.*?)rel=[\"']nofollow[\"'](.*?)>/i";
$content = preg_replace($pattern, '<a$1href="' . $site_url . '$2"$3$4>', $content);
return $content;
}
add_filter('the_content', 'remove_nofollow_from_internal_links');
  1. Click “Update File” to save the changes.

This code snippet will add the “nofollow” attribute to all links in your site’s content. However, please be aware that editing your theme’s functions.php file can have unintended consequences if not done correctly. It’s always a good idea to create a backup of your site before making changes to the code.

Use a plugin:

If you prefer not to modify your theme’s functions.php file, you can use a plugin to add the “nofollow” attribute to all links in your posts. Some plugins that can help you with this task include “Nofollow Manager,” “Nofollow Control,” and “Ultimate Nofollow.”
To install a plugin:

  1. Log in to your WordPress admin dashboard.
  2. Go to “Plugins” > “Add New.”
  3. Search for one of the plugins mentioned above.
  4. Install and activate the plugin.
  5. Configure the plugin settings according to your needs (each plugin may have slightly different settings).

Using a plugin can be a safer and more convenient option, as it does not involve editing your theme’s code directly.

Leave a Reply

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