A friend recently encountered an unsettling issue with their WordPress posts while using Yoast SEO Premium combined with the Classic Editor. They discovered that Yoast had been automatically injecting peculiar CSS classes, such as ai-optimize-6
and ai-optimize-9
, directly into their content. The complication arose when these classes persisted within the posts, even after either disabling the Yoast AI Optimize feature or uninstalling the plugin altogether. This unexpected behavior contradicts the typical plugin performance, as an uninstalled plugin generally should not leave any remnants in the content.
Though these AI markers don’t visually alter the appearance of the site, they do contribute to unnecessary clutter in the source code. Furthermore, they could potentially indicate to AI content detectors, plagiarism checkers, and search engines that the content had either been generated or optimized by an AI, impacting its credibility and performance. This guide aims to provide you with a solution to eliminate these hidden classes using a simple code snippet.
Understanding the SEO Impact of the ai-optimize Classes
The ai-optimize-{number}
CSS classes are added when employing Yoast SEO Premium’s AI features with the Classic Editor. While they remain invisible on the front end, they are present in the HTML of your content, potentially causing various disturbances.
The reasons for removing these classes are compelling:
- Cluttered HTML: These classes can make your code cumbersome, hindering readability and parsing.
- Redundant: They do not contribute functionality or visual appeal, merely existing as remnants from the AI tool.
- AI Detection Risk: Various plagiarism checkers and AI content detectors may identify these patterns, possibly flagging your work as AI-generated.
- Unwanted AI Signatures: The presence of these classes spread across multiple sites could lead Google to associate this pattern with low-quality, AI-generated content.
- Formatting Conflicts: Unfamiliar classes might interfere with your themes or plugins in the future.
Given the lack of advantages and significant downsides, it’s prudent to remove these markers.
Step-by-Step Guide to Remove the Classes
1. Backup Your Site
Before making any changes, it is crucial to create a complete backup of your WordPress site. This simple step provides peace of mind should any errors occur during the editing process. Using a reliable backup plugin can streamline this task.
2. Implement the Code Snippet
Once you have backed up your site, you can proceed to cleanse your posts of the ai-optimize
classes. The following code, which works seamlessly with both the Classic Editor and the Block Editor (Gutenberg), is recommended.
You will benefit from utilizing a code snippet management plugin, which allows for safe code addition without risking important site functions.
// For Classic Editor and programmatic updatesfunction 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 Editorfunction 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 operationsfunction 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 operationsfunction 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 classesfunction 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 inserting this code, selecting the ‘Run Everywhere’ setting will ensure it is active throughout your WordPress site. This will automatically strip any newly created or updated posts of the unwanted classes.
3. Bulk Update Existing Posts
To remove the ai-optimize
classes from previously published posts, bulk editing is required. Access the Posts section in your WordPress dashboard, set the posts per page to a maximum of 999, and select them all. Using the Bulk Actions dropdown, just click ‘Update’ without making any other changes. This process will trigger the code you inserted earlier, effectively cleansing your existing content.
Consider an Alternative SEO Plugin
While Yoast SEO has served many well, it may be time to explore other options. By transitioning to an alternative SEO plugin like All in One SEO, users gain access to a broader range of features and potentially improved performance in the current digital landscape.
Additional Resources
As you consider switching or enhancing your SEO strategy, take a look at various resources available to further you on your journey. From guides on migrating away from Yoast to comprehensive SEO strategies, the right information can streamline your process and boost your results.
By following this guide, you can successfully resolve the ai-optimize
class issue in Yoast SEO, setting your site up for the best long-term performance. If challenges arise, professional assistance is available for safe and effective WordPress support.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.