Yasir Khan
Yasir Khan

Reputation: 520

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.

Upvotes: 0

Views: 1683

Answers (4)

subash pandey
subash pandey

Reputation: 63

//w3c error wordpress
//we use 'init' action to use ob_start()
add_action( 'init', 'process_post' );

function process_post() {
     ob_start();
}


add_action('shutdown', function() {
    $final = '';

    // We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
    // that buffer's output into the final output.
    $levels = ob_get_level();

    for ($i = 0; $i < $levels; $i++) {
        $final .= ob_get_clean();
    }

    // Apply any filters to the final output
    echo apply_filters('final_output', $final);}, 0);add_filter('final_output', function($output) {
    //this is where changes should be made    $find=array('type="rocketlazyloadscript"',"type='rocketlazyloadscript'",'type="text/javascript"',"type='text/javascript'",'type="application/javascript"',"type='application/javascript'",'type="text/css"',"type='text/css'" );
    return str_replace($find, '', $output);});
function theme_name_setup() {
    add_theme_support( 'html5', array( 'script', 'style' ) );}
add_action( 'after_setup_theme', 'theme_name_setup' );    

Source code

Upvotes: 0

cyberfunk
cyberfunk

Reputation: 9

I added CSS to the function and it solved my JS and CSS W3C validation warnings. Please let me know if you see errors.

//Remove JS and CSS types
add_action( 'template_redirect', function(){
ob_start( function( $buffer ){
    $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'", 'type="text/css"', "type='text/css'" ), '', $buffer );

    return $buffer;
});
});

Upvotes: -1

Xhynk
Xhynk

Reputation: 13880

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.

Upvotes: 2

Riad Citaku
Riad Citaku

Reputation: 349

This is how you remove type="text/javascript" from all JavaScript files with the WordPress filter script_loader_tag.

add_filter('script_loader_tag', 'your_slug_remove_type_attr', 10, 2);
function your_slug_remove_type_attr($tag, $handle) {
    return preg_replace("/type=['\"]text\/(javascript)['\"]/", '', $tag);
}

Upvotes: 2

Related Questions