Support » Plugin: Autoptimize » How to remove type attributes?

  • Resolved Felix Scholze

    (@felix-berlin-design)


    Hi,
    i tried to remove the type attribute from the style and script tags with the following code snippet:

    //* Remove type tag from script and style
    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 );
    }

    In the un-cached version it works. It seems so that autoptimize add those attributes to the cached website.

    On validator.w3.org i get some warnings because those attributes are no longer needed.
    Examples:

    Warning: The type attribute for the style element is not needed and should be omitted.

    Warning: The type attribute is unnecessary for JavaScript resources.

    The page I need help with: [log in to see the link]

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Frank Goossens

    (@futtta)

    you’ll have to hook into the autoptimize_html_after_minify filter instead @felix-berlin-design 🙂

    hope this helps,
    frank

    Felix Scholze

    (@felix-berlin-design)

    Hi Frank,
    thanks that works!

    Plugin Author Frank Goossens

    (@futtta)

    great! feel free to leave a raving review here! 😉

    Can anyone help with how to use the autoptimize_html_after_minify to remove the type from the javascript tag please?

    Felix Scholze

    (@felix-berlin-design)

    You have to add the following filter:
    add_filter('autoptimize_html_after_minify', 'codeless_remove_type_attr', 10, 2);

    This this the whole function + filters:

    //* Remove type tag from script and style
    add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2);
    add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2);
    add_filter('autoptimize_html_after_minify', 'codeless_remove_type_attr', 10, 2);
    function codeless_remove_type_attr($tag, $handle)
    {
        return preg_replace("/type=['\"]text\/(javascript|css)['\"]/", '', $tag);
    }
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    looking good Felix, thanks! 🙂

    Hi guys,

    I tried using the code above in my localhost PHP v7.2 but I got an error. Is there any possible solutions for this?

    Thank you in advance!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    well, what error did you get?

    <b>Fatal error</b>:  Uncaught ArgumentCountError: Too few arguments to function codeless_remove_type_attr(), 1 passed in Z:\xampp\htdocs\site\wp-includes\class-wp-hook.php on line 286 and exactly 2 expected in Z:\xampp\htdocs\site\wp-content\themes\site\functions.php:537
    Stack trace:
    #0 Z:\xampp\htdocs\site\wp-includes\class-wp-hook.php(286): codeless_remove_type_attr('<!doctype html>...')
    #1 Z:\xampp\htdocs\site\wp-includes\plugin.php(203): WP_Hook->apply_filters('<!doctype html>...', Array)
    #2 Z:\xampp\htdocs\site\wp-content\plugins\autoptimize\classes\autoptimizeMain.php(455): apply_filters('autoptimize_htm...', '<!doctype html>...')
    #3 [internal function]: autoptimizeMain->end_buffering('<!doctype html>...', 9)
    #4 Z:\xampp\htdocs\site\wp-includes\functions.php(3743): ob_end_flush()
    #5 Z:\xampp\htdocs\site\wp-includes\class-wp-hook.php(286): wp_ob_end_flush_all('')
    #6 Z:\xampp\htdocs\site\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
    #7 Z:\xampp\htdocs\site\wp-includes\plugin.php(453): WP_Hook in <b>Z:\xampp\htdocs\site\wp-content\themes\site\functions.php</b> on line <b>537</b><br />

    please see above, thanks!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    OK, can you change the code snippet into;

    add_filter('autoptimize_html_after_minify', 'codeless_remove_type_attr', 10, 1);
    function codeless_remove_type_attr($tag)
    {
        return preg_replace("/type=['\"]text\/(javascript|css)['\"]/", '', $tag);
    }

    Problem solved. Thanks!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    you’re welcome @cmgeoogz, feel free to leave a review of the plugin and support here! 🙂

    You can modify the @felix-berlin-design code to this to make it work for all hooks:

    (made $handle=”)

    //* Remove type tag from script and style
    add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2);
    add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2);
    add_filter('autoptimize_html_after_minify', 'codeless_remove_type_attr', 10, 2);
    function codeless_remove_type_attr($tag, $handle='')
    {
        return preg_replace("/type=['\"]text\/(javascript|css)['\"]/", '', $tag);
    }
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to remove type attributes?’ is closed to new replies.