Automatically Remove The Grammarly Code Bloat in WordPress

Updated 02/07/2024, Posted 02/07/2024 by James Parsons James Parsons 2 Comments

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 Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.