Fix Yoast SEO’s ai-optimize Bug Before It Ruins Your Site’s SEO
A friend recently contacted me after uncovering something concerning in their WordPress posts. They were using Yoast SEO Premium with the Classic Editor and discovered that Yoast had been automatically inserting peculiar CSS classes like ai-optimize-6
, ai-optimize-9
, directly into their content.
The issue is that these classes remain permanently embedded in the posts, even after disabling Yoast AI Optimise or entirely uninstalling the plugin. This contradicts the expected behaviour of plugins… that is, when you uninstall them, they should leave no traces in your content.
While these AI markers may not visually impact your site, they clutter your source code. This could potentially alert AI content detectors, plagiarism checkers, and even search engines to the fact that your content was AI-generated or optimised.
In this guide, I’ll demonstrate how to remove these hidden classes using a simple code snippet. I’ll also explain how to implement it safely and share the SEO plugin I recommend as an alternative to Yoast.

Here are the topics I will cover in this tutorial:
Why These AI-Optimise Classes Are Detrimental for SEO
The ai-optimize-{number}
CSS classes are inserted when you utilise Yoast SEO Premium’s AI features with the Classic Editor. They do not appear on the front end, but they are embedded in the HTML of your content, which can lead to issues.
You can view them by visiting any post or page on your site and using the Inspect tool in your browser.

Here’s why I suggest removing them:
- They clutter your HTML: These unnecessary classes make your code more difficult to read and parse.
- They serve no purpose: They do not influence how your content looks or operates. They are merely remnants from the AI tool.
- They can trigger AI detection tools: Some plagiarism checkers and AI content detectors may identify these patterns and flag your post, even if you authored it yourself.
- They leave AI footprints across your site: If multiple sites utilise the same classes, Google may start linking that pattern to low-quality or mass-produced AI content.
- They increase the risk of formatting conflicts: Unknown classes could interfere with your theme or plugins in the future.
There’s no benefit to retaining these hidden markers, and several compelling reasons to eliminate them.
The good news is that there’s a quick solution, and I’ll show you how to do it safely in the next section.
Step 1: Create a Backup Before Making Changes
Before we proceed, I always recommend making a full backup of your WordPress site. It only takes a few minutes and offers peace of mind in case anything goes awry.
I use Duplicator when I require a swift and dependable backup solution. It’s the best WordPress backup plugin available, it’s beginner-friendly, and it functions well whether you’re backing up or migrating your site.
- On-demand and automatic WordPress backups
- Securely stored in remote locations like Dropbox or Google Drive
- Easy 1-click restore if anything breaks
For details, see our guide on how to back up your WordPress website.
Once your backup is prepared, you’re ready to move on to the next step, where I will show you how to resolve the issue.
Step 2: Insert the Code Snippet to Remove ai-optimize Classes
Now that your backup is prepared, it’s time to clean up those ai-optimize-{number}
and ai-optimize-introduction
classes.
I’ve compiled a secure and adaptable code snippet that works with both the Classic Editor and the Block Editor (Gutenberg), as well as bulk edits.
You need not access your theme files or experiment with FTP. Instead, I recommend using the WPCode plugin to add this snippet. It’s what I use to manage code snippets on WordPress sites without jeopardising anything important. (See my comprehensive WPCode review for further details.)
Tip: WPCode offers a limited free version that you can utilise for this tutorial. However, I recommend upgrading to a paid plan to unlock its full capabilities.
If this is your first time adding custom code to your site, then you may wish to consult our guide on how to add custom code snippets in WordPress without breaking your site.
First, you need to install and activate the WPCode plugin. Refer to our tutorial on installing a WordPress plugin if you require assistance.
Once the plugin has been activated, navigate to the Code Snippets » + Add Snippet page and click on the ‘+ Add Custom Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ box.

Next, you’ll need to provide a title for your code snippet. This could be anything that helps you easily identify this code.
Following that, select PHP Snippet from the ‘Code Type’ drop-down menu.

Now, you need to copy and paste the following code into the Code Preview box.
Here’s the full code snippet:
// For Classic Editor and programmatic updates
function strip_ai_optimize_classes($data, $postarr) {
if (empty($data['post_content']) || $data['post_type'] !== 'post') {
return $data;
}
$data['post_content'] = strip_ai_optimize_from_content($data['post_content']);
return $data;
}
add_filter('wp_insert_post_data', 'strip_ai_optimize_classes', 10, 2);
// For Gutenberg/Block Editor
function strip_ai_optimize_classes_rest_insert($prepared_post, $request) {
if (isset($prepared_post->post_content) && $prepared_post->post_type === 'post') {
$prepared_post->post_content = strip_ai_optimize_from_content($prepared_post->post_content);
}
return $prepared_post;
}
add_filter('rest_pre_insert_post', 'strip_ai_optimize_classes_rest_insert', 10, 2);
// For bulk edit operations - this is the key addition
function strip_ai_optimize_classes_bulk_edit($post_id) {
$post = get_post($post_id);
if (!$post || empty($post->post_content) || $post->post_type !== 'post') {
return;
}
$cleaned_content = strip_ai_optimize_from_content($post->post_content);
if ($cleaned_content !== $post->post_content) {
remove_action('post_updated', 'strip_ai_optimize_classes_bulk_edit');
wp_update_post(array(
'ID' => $post_id,
'post_content' => $cleaned_content
));
add_action('post_updated', 'strip_ai_optimize_classes_bulk_edit');
}
}
add_action('post_updated', 'strip_ai_optimize_classes_bulk_edit');
// Catch bulk operations via the bulk_edit_posts action
function strip_ai_optimize_classes_bulk_action($post_ids) {
if (!is_array($post_ids)) {
return;
}
foreach ($post_ids as $post_id) {
strip_ai_optimize_classes_bulk_edit($post_id);
}
}
add_action('bulk_edit_posts', 'strip_ai_optimize_classes_bulk_action');
// Shared function to strip ai-optimize classes
function strip_ai_optimize_from_content($content) {
if (empty($content) || !is_string($content)) {
return $content;
}
return preg_replace_callback(
'/class\s*=\s*["\']([^"\']*)["\']/',
function($matches) {
$classes = $matches[1];
$classes = preg_replace('/\bai-optimize-\d+\b\s*/', '', $classes);
$classes = preg_replace('/\s+/', ' ', trim($classes));
if (empty($classes)) {
return '';
}
return 'class="' . $classes . '"';
},
$content
);
}
After adding the code, scroll down to the ‘Insertion’ section.
Then, select ‘Run Everywhere’ next to the ‘Location’ option.

Finally, navigate to the top of the page and switch the status toggle in the top-right corner to Active, and then click on the ‘Save Snippet’ button to save your changes.
Once you’ve added this snippet to your site using WPCode, it will automatically remove these AI-generated classes from any post you create or update in the future.
If you wish to eliminate the AI classes from existing content, you will need to bulk edit your current posts.
Expert Tip: If you’re not comfortable editing code yourself, don’t worry!
Our team at Qureshi offers Emergency WordPress Support Services to assist you in quickly and safely resolving such issues. We can tidy up your content and set up your SEO plugin correctly.
Step 3: Bulk Update All Posts to Clean Up Existing AI Classes
Now that the code snippet is in place, it will automatically rid your posts of any AI markers when you edit or publish them. However, to remove these classes from your older posts, you’ll need to perform a bulk update.
Don’t fret—this won’t alter your content. It simply triggers the filter we just added so the hidden AI classes can be removed safely.
First, navigate to the Posts » All Posts page in your WordPress dashboard and click ‘Screen Options’ at the top right.

From here, adjust the number of posts displayed per page to 999 (this is the maximum number you can show on this screen) and click ‘Apply’ to load all your posts.
Next, select all posts on the page by clicking the top checkbox. After that, choose ‘Edit’ from the Bulk Actions dropdown, then click ‘Apply’.

WordPress will now present you with bulk editing options. Without changing anything else, simply click on the ‘Update’ button.
WordPress will now commence updating all your posts. By doing this, it will also trigger the code you saved earlier and eliminate the AI classes.

Tip : If you have more than 999 posts, simply navigate to the next page and repeat this process until all posts have been updated.
This will cleanse the ai-optimize-{number}
and ai-optimize-introduction
classes from all your existing posts—no manual editing required.
Bonus Tip: Switching to an Alternative SEO Plugin (Superior and More Comprehensive)
Yoast SEO has been established for quite some time, but recently its innovations have stagnated.
At Qureshi, we decided to transition to All in One SEO across all our sites a few years ago. It was a significant decision, and we documented every reason in this case study: Why We Switched from Yoast to All in One SEO.

I now use All in One SEO for every personal project and for all client websites. It is my preferred SEO plugin due to its:
- Comprehensive features for the AI search era (schema markup, advanced sitemaps, AI integrations, and more)
- Easy setup with intelligent defaults and checklists
- Superior support for local SEO, WooCommerce, Google News, and more.
If you’re still uncertain, we’ve prepared a detailed side-by-side comparison here: Yoast SEO vs All in One SEO – Which Is the Better Plugin?
Important : If you’ve upgraded to the latest version of Yoast SEO Premium (version 25.3.1 or later) or switched to All in One SEO, you can now simply disable the code snippet in WPCode.
Just navigate to the Code Snippets page in the WordPress admin area and toggle the switch next to the snippet to disable it.

Bonus SEO Resources
Whether you are transitioning away from Yoast SEO or simply seeking to enhance your WordPress SEO strategy, here are some invaluable resources to assist you.
These tutorials and comparisons can save you time, help avoid costly errors, and enable you to achieve better outcomes from your SEO efforts: