1

I am working on WordPress Site,these warnings are coming on every single page, Is there any method to resolve this.

Warning:The type attribute is unnecessary for JavaScript resources. <script type="text/javascript">

2

4 Answers 4

5

As of WordPress 5.3 you can add 'style' and 'script' to add_theme_support for HTML5 to remove the type attributes.

function register_html_support() {
    add_theme_support( 'html5', array( 'script', 'style' ) );
}
add_action( 'after_setup_theme', 'register_html_support' );
Sign up to request clarification or add additional context in comments.

Comments

4

Add following code to your theme's functions.php

add_action( 'template_redirect', 'prefix_remove_template_redirect', 10, 2);

function prefix_remove_template_redirect(){
  ob_start( function( $buffer ){
    $buffer = str_replace( array( 
        '<script type="text/javascript">',
        "<script type='text/javascript'>", 
        "<script type='text/javascript' src=",
        '<script type="text/javascript" src=',
        '<style type="text/css">', 
        "' type='text/css' media=", 
        '<style type="text/css" media',
        "' type='text/css'>"
    ), 
    array(
        '<script>', 
        "<script>", 
        "<script src=",
        '<script src=',
        '<style>', 
        "' media=", 
        '<style media',
        "' >"
    ), $buffer );

    return $buffer;
  });
};

This will change <script type="text/javascript"> to <script> And will change <style type="text/css"> to <style>. It can be done by script_loader_tag but it’s given theme check validation error.


Update: From WordPress 5.3, You can use following.

function theme_name_setup() {
    add_theme_support( 'html5', array( 'script', 'style' ) );
}
add_action( 'after_setup_theme', 'theme_name_setup' );

4 Comments

This broke my post: displays a blank screen.
It should work. I used that code in my projects. and it's working.
@SherylHohman remove numbers 8798 from function's name. it works for me. thanks @JahirulIslamMamun
Works good and with plugins on custom built themes. Easy to understand and modify for your needs. Thanks.
2

Add the below code to your theme's functions.php

add_filter('script_loader_tag', 'clean_script_tag');
function clean_script_tag($input) {
  $input = str_replace("type='text/javascript' ", '', $input);
  return str_replace("'", '"', $input);
}

This will change <script type="text/javascript"> to <script> for the plugin loaded scripts.

And then change <script type="text/javascript"> to <script> in your theme's header an footer php files.

7 Comments

I used this , but still it shows type attribute with script tag
I am not able to find why this code is not running on my project
How you doing ? What themes you are using? What is your WordPress Version?
Looks like you need to manually search in your plugins as wel as in your theme
I am creating my own theme, working on latest version of WordPress 4.9.6
|
0

i added two things. This works for me:

#one for JS mime-type
add_action( 'template_redirect', function(){
  ob_start( function( $buffer ){
       $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'" ), '', $buffer );        
       return $buffer;
    });
});
#one for CSS-Mime-Type
add_action( 'template_redirect', function(){
    ob_start( function( $buffer ){
        $buffer = str_replace( array( 'type="text/css"', "type='text/css'" ), '', $buffer );        
        return $buffer;
    });
});

I know you can do both in one function, but here can you decide for oneself what you want (JS or CSS or both)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.