How to Remove class="wp-block-heading" from Heading tags?

WordPress started adding class="wp-block-heading" to headings recently, from WordPress 6.2, to be more specific. Here's the announcement post.

DevTools gauravtiwari.orgcontent marketing strategies for saas companies 2024 02 004036

This is a welcome change for most, who wanted to customize or hook into WordPress's default headings for more freedom but some of us might want to just get rid of it.

How to remove class="wp-block-heading" from Headings?

Here is how you can do this using a simple WordPress code snippet.

function gauravt_remove_wp_block_heading_class_from_headings($content) {
    // Use regular expression to find and remove the class="wp-block-heading" from h1 to h6 tags
    $pattern = '/<(h[1-6])\s+class="wp-block-heading"(.*?)>/i';
    $replacement = '<$1$2>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
}

// Add the filter to 'the_content' hook
add_filter('the_content', 'gauravt_remove_wp_block_heading_class_from_headings');

Where to place this code?

You can place this code:

  1. Either in your theme's functions.php file. Do this to preferably the Child Theme's functions.php file as theme updates override any changes the user makes.
  2. Or in a functionality plugin like Code Snippets, WP Code, etc.
Edit Snippet ‹ Gaurav Tiwari — WordPress 2024 02 004042

Performance Impact

DevTools gauravtiwari.orgcontent marketing strategies for saas companies 2024 02 004036
Before
DevTools gauravtiwari.orgcontent marketing strategies for saas companies 2024 02 004040 1
After

There isn't any* performance impact if you use this code snippet. It just uses the the_content filter hook to make the changes before it's sent to the browser. No database changes are made.

Note that this approach removes all instances of class="wp-block-heading" from the post content, which might affect styling if other elements rely on this class.

Always test changes like these in a staging environment before applying them to a live site to avoid unexpected results.