As a WordPress developer, I understand the importance of clean, efficient code. That’s why I want to share my recent discovery on removing id attributes from link and script tags in WordPress.

Id attributes in HTML code can cause unnecessary bloat and slow down the site’s performance. That’s why I created a function to remove them and improve our WordPress site’s speed and maintainability. Here’s the function I wrote:

function remove_style_id_attribute( $tag, $handle ) {
    return preg_replace( '/id=["\']?([^"\'\s]+)["\']?/', '', $tag );
add_filter( 'style_loader_tag', 'remove_style_id_attribute', 10, 2 );
add_filter( 'script_loader_tag', 'remove_style_id_attribute', 10, 2 );

This function uses a regular expression to search for and remove id attributes from link and script tags. To make it run whenever a link or script tag is loaded on the site, I hooked it to the style_loader_tag filter using the add_filter function. The filter’s priority is set to 10, and it accepts two arguments, $tag and $handle. The $tag argument represents the HTML tag being filtered, and the $handle argument represents the handle used to register the tag.

By removing id attributes, we can clean up the HTML code and improve our site’s performance and maintainability. I hope this information is helpful to other WordPress developers looking to optimize their sites.

More information can be found in the documentation

Best regards Sozonenko Anton. Goodbye!