A friend reached out to me recently after discovering something alarming in their WordPress posts. They were using Yoast SEO Premium with the Classic Editor, and they found Yoast had been automatically inserting odd-looking CSS classes like ai-optimize-6
, ai-optimize-9
, directly into their content.
The problem is that these classes remain permanently embedded in the posts even after disabling Yoast AI Optimize or completely deleting the plugin. This goes against expected plugin behavior… that is, when you uninstall it, it should leave no trace in your content.
While these AI markers might not visually affect your site, they clutter up your source code. It could also potentially signal to AI content detectors, plagiarism checkers, and even search engines that your content was AI-generated or optimized.
In this guide, I’ll show you how to remove these hidden classes using a quick code snippet. I’ll also explain how to apply it safely and share the SEO plugin I recommend using as an alternative to Yoast.
Here are the things I will cover in this tutorial:
Why These ai-optimize Classes Are Bad for SEO
The ai-optimize-{number}
CSS classes are added when you use Yoast SEO Premium’s AI features with the Classic Editor. They don’t appear on the front end, but they’re embedded in your content’s HTML, which can cause problems.
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 recommend removing them:
- They clutter your HTML: These unnecessary classes make your code harder to read and parse.
- They serve no purpose: They don’t affect how your content looks or functions. They’re just leftovers from the AI tool.
- They can trigger AI detection tools: Some plagiarism checkers and AI content detectors pick up these patterns and may flag your post, even if you wrote it yourself.
- They leave AI footprints across your site: If multiple sites use the same classes, Google might start associating that pattern with low-quality or mass-produced AI content.
- They increase the risk of formatting conflicts: Unknown classes could interfere with your theme or plugins down the road.
There’s no upside to keeping these hidden markers, and several good reasons to clean them out.
The good news is that there is a quick fix, and I’ll show you how to do it safely in the next section.
Step 1: Make a Backup Before Making Changes
Before we move forward, I always recommend creating a full backup of your WordPress site. It only takes a few minutes and gives you peace of mind in case anything goes wrong.
I use Duplicator when I need a quick and reliable backup solution. It’s the best WordPress backup plugin on the market, it is beginner-friendly, and it works great whether you’re backing up or migrating your site.
- ✅ On-demand and automatic WordPress backups
- ✅ Safely stored in remote locations like Dropbox or Google Drive
- ✅ Easy 1-click restore if something breaks
For details, see our guide on how to back up your WordPress website.
Once your backup is ready, you’re safe to move on to the next step, where I will show you how to fix the problem.
Step 2: Add the Code Snippet to Remove ai-optimize Classes
Now that your backup is ready, it’s time to clean up those ai-optimize-{number}
and ai-optimize-introduction
classes.
I’ve put together a safe and flexible code snippet that works with both the Classic Editor and the Block Editor (Gutenberg), as well as bulk edits.
You don’t need to touch your theme files or mess 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 risking anything important. (See my full WPCode review for more details.)
Tip: WPCode has a limited free version that you can use for this tutorial. However, I recommend upgrading to a paid plan to unlock its full potential.
If this is your first time adding custom code to your site, then you can take a look at 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. See our tutorial on installing a WordPress plugin if you need help.
Once the plugin has been activated, go to the Code Snippets » + Add Snippet page and click on ‘+ Add Custom Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ box.
Next, you need to provide a title for your code snippet. This could be anything that helps you identify this code easily.
After that, choose 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(
'/classs*=s*["']([^"']*)["']/',
function($matches) {
$classes = $matches[1];
$classes = preg_replace('/bai-optimize-d+bs*/', '', $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, go to the top of the page and switch the status toggle in the top-right to Active, and then click on the ‘Save Snippet’ button to store your changes.
Once you’ve added this snippet to your site using WPCode, it will automatically strip these AI-generated classes from any post you create or update in the future.
If you want to remove the ai-classes from existing content, you’ll have to bulk edit your existing content.
🌟Expert Tip: If you’re not comfortable editing code yourself, don’t stress!
Our team at WPBeginner offers Emergency WordPress Support Services to help you fix issues like this quickly and safely. We can clean up your content and set up your SEO plugin the right way.
Step 3: Bulk Update All Posts to Clean Up Existing AI Classes
Now that the code snippet is in place, it will automatically clean up any AI markers when you edit or publish a post. But to remove these classes from your older posts, you’ll need to bulk update them.
Don’t worry—this won’t change your content. It simply triggers the filter we just added so the hidden AI classes can be stripped out safely.
First, you need to go to the Posts » All Posts page in your WordPress dashboard and click ‘Screen Options’ at the top right.
From here, set the number of posts per page to 999 (This is the maximum number of posts 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, select ‘Edit’ by clicking on the Bulk Actions dropdown, then click ‘Apply’.
WordPress will now show you bulk editing options. Without changing anything else, simply click on the ‘Update’ button.
WordPress will now start updating all your posts. By doing this, it will also trigger the code you saved earlier and remove the AI classes.
Tip 💡: If you have more than 999 posts, just go to the next page and repeat this process until all posts have been updated.
This will clean the ai-optimize-{number}
and ai-optimize-introduction
classes from all your existing posts—no manual editing needed.
Bonus Tip: Switching to an Alternative SEO Plugin (Better and More Powerful)
Yoast SEO has been around for a long time, but lately, its innovations have slowed down.
At WPBeginner, we made the decision to switch to All in One SEO across all our sites a few years ago. It was a big move, 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 on every personal project and all client websites. It’s my go-to SEO plugin because it offers:
- ✅ Comprehensive features for the AI search era (schema markup, advanced sitemaps, AI integrations, and more)
- ✅ Easy setup with smart defaults and checklists
- ✅ Better support for local SEO, WooCommerce, Google News, and more.
If you’re still on the fence, we’ve made a detailed side-by-side breakdown here: Yoast SEO vs All in One SEO – Which Is the Better Plugin?
Bonus SEO Resources
Whether you’re switching away from Yoast SEO or just want to tighten up your WordPress SEO strategy, here are some helpful resources to guide you.
These tutorials and comparisons can save you time, avoid costly mistakes, and help you get better results from your SEO efforts:
I hope this guide helped you fix the ai-optimize
class issue in Yoast SEO and set your site up for better long-term results. You’ve got this—and if you ever need a hand, we’re here to help.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.