Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove type attribute from script and style tags added by WordPress plugins

Tags:

php

wordpress

I am using the function below to remove type attribute from a script and style tags but this function doesn't remove the type attribute from the scripts/styles added by the plugins. It only works on my theme files.

add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2);

function codeless_remove_type_attr($tag, $handle) {
    return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}
like image 606
Elichy Avatar asked Dec 10 '22 06:12

Elichy


1 Answers

The following code worked for me, please try this by pasting the code into you function.php file.

add_action('wp_loaded', 'prefix_output_buffer_start');
function prefix_output_buffer_start() { 
    ob_start("prefix_output_callback"); 
}

add_action('shutdown', 'prefix_output_buffer_end');
function prefix_output_buffer_end() { 
    ob_end_flush(); 
}

function prefix_output_callback($buffer) {
    return preg_replace( "%[ ]type=[\'\"]text\/(javascript|css)[\'\"]%", '', $buffer );
}

Hope this will work for you too.

like image 180
Tristup Avatar answered Feb 13 '23 04:02

Tristup