...
Tutorials, Wordpress

How to remove type attribute from script and style tags added by WordPress

07 February 2019

Here we are going to learn how to remove type attribute from script and style tag added by wordpress.

You can always go all-out and use output buffering:

add_action('wp_loaded', 'output_buffer_start');
function output_buffer_start() { 
    ob_start("output_callback"); 
}

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

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

Description of above two hooks the first Output buffering is turned on at the start (wp_loaded hook), right as wp + theme + plugins + etc are fully loaded, and the second is turned off at the last moment (shutdown hook) which runs just before PHP shuts down execution. The regex must work through everything, and that could be a lot of content.

Leave a Comments