Search code examples
phpwordpresscontact-form-7

Remove type='text/javascript' from contact form 7 tags


I removed most of the type attributes from my wordpress site but still I can see this on the contact form 7 tags, how do I remove them?

I was searching this on google but didn't find the solution. Please help!

Note: I am validating html on validator.w3.org it gives warning, please see below.

Warning: The type attribute is unnecessary for JavaScript resources.

Solution

  • You've mentioned that your client wants these tags removed. I assume you've explained to him that they're benign (and arguably sometimes helpful). Barring that, if you still need to remove these tags, you can just use a "run time replace" instead of trying to find every. single. instance. of a script that has that attribute, especially if one plugin or another doesn't enqueue scripts properly.

    Take this function for instance:

    add_action( 'template_redirect', function(){
        ob_start( function( $buffer ){
            $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'" ), '', $buffer );
    
            return $buffer;
        });
    });
    

    The template_redirect hook effectively has access to all of the markup for the page. So you can instead just add it to an Output Buffer, run a simple str_replace on it, and return that output.

    You'll notive I've added both quote notation types since either could be in your source code at some point. You could use a regex for it if you wanted, but the array of both notations should suffice.