Automatically Remove The Grammarly Code Bloat in WordPress

James Parsons by James Parsons Updated Feb 7th, 2024

When pasting in content from Grammarly into WordPress, you see all kinds of new junk added:

  • <a class="editor-rtfLink" ...
  • <a rel="noopener" ...
  • <span data-preserver-spaces="true"...

This is carried over from one rich text editor (Grammarly) into another (WordPress). Most of these are just extra classes and span tags, which, at most, are just footprints that show that you use Grammarly to search engines and savvy users. But, it is extra bloat and junk added to your post that you don't need.

Here's some code to add to your functions.php that will automatically remove these Grammarly tags and footprints before your content is displayed on the frontend:

/*
 * Strips Grammarly junk from the frontend of the website
 */ 
 
function cp_remove_link_attributes($content) {
  $content = preg_replace('/<a(.*?)class="editor-rtfLink"(.*?)>/i', '<a$1$2>', $content);
  $content = preg_replace('/<a(.*?)rel="noopener"(.*?)>/i', '<a$1$2>', $content);
  $content = preg_replace('/<span data-preserver-spaces="true">(.*?)<\/span>/', '$1', $content);
  return $content;
}
add_filter('the_content', 'cp_remove_link_attributes');

This works by removing this bloat before WordPress serves content to visitors and search engines. So, it will still exist in your WordPress dashboard when viewing your posts, but not on the frontend of your website. Since it isn't actually editing your posts, this is safe and completely reversible.

If you want to actually remove these tags on the backend as well so that you aren't seeing them in the WordPress code editor, you can use this version. However, you should take caution, as this is actually modifying your content each time you save or update a post:

/*
 * Check and remove Grammarly junk from WordPress posts when saving
 */ 
 
 function cp_remove_link_attributes_on_save($post_id) {
    if (wp_is_post_revision($post_id) || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) {
        return;
    }
    $post = get_post($post_id);
    $updated_content = preg_replace('/<a(.*?)class="editor-rtfLink"(.*?)>/i', '<a$1$2>', $post->post_content);
    $updated_content = preg_replace('/<a(.*?)rel="noopener"(.*?)>/i', '<a$1$2>', $updated_content);
    $updated_content = preg_replace('/<span data-preserver-spaces="true">(.*?)<\/span>/', '$1', $updated_content);
    if ($updated_content !== $post->post_content) {
        remove_action('save_post', 'cp_remove_link_attributes_on_save');
        wp_update_post([
            'ID' => $post_id,
            'post_content' => $updated_content
        ]);
        add_action('save_post', 'cp_remove_link_attributes_on_save');
    }
}
add_action('save_post', 'cp_remove_link_attributes_on_save');

This will only run when you publish a new post or update an existing post - and it will only run on the post that you're currently working on.

That means that if you need to apply this to your older posts as well, you need use the WordPress bulk editor. Try changing something that is easily reversible, like switching the author of your posts to a different author and then switching it again back to the original. This bulk update will trigger this code on all of your posts at once, effectively removing Grammarly code from all of them. Please test on one post first to ensure it's working well in your environment.

You only have to run this manually one time for your older posts. Then, all new posts will automatically remove this Grammarly junk in the background as soon as you save, update, or publish a post. Cool, right?

What do you think? Did this help you? Have you found a better way to handle this? Please share in the comments section!

Related Code Entries

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.