Last active
December 19, 2018 10:44
-
-
Save damiencarbery/fc1965d5e4728cc93b428b9c2a7fbea2 to your computer and use it in GitHub Desktop.
Remove 'type' attribute from 'script' and 'style' tags to reduce W3C Validator warnings: https://www.damiencarbery.com/2018/11/remove-type-from-script-and-style-markup/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Remove type from script and style tags | |
Plugin URI: https://www.damiencarbery.com/2018/11/remove-type-from-script-and-style-markup/ | |
Description: Remove 'type="text/javascript"' from 'script' tags and 'type="text/css"' from 'style' tags. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.2 | |
*/ | |
add_filter( 'script_loader_tag', 'dcwd_remove_type', 10, 3 ); | |
add_filter( 'style_loader_tag', 'dcwd_remove_type', 10, 3 ); // Ignore the $media argument to allow for a common function. | |
function dcwd_remove_type( $markup, $handle, $href ) { | |
//error_log( 'Markup: ' . $markup ); | |
//error_log( 'Handle: ' . $handle ); | |
//error_log( 'Href: ' . $href ); | |
// Remove the 'type' attribute. | |
$markup = str_replace( " type='text/javascript'", '', $markup ); | |
$markup = str_replace( " type='text/css'", '', $markup ); | |
return $markup; | |
} | |
// Store and process wp_head output to operate on inline scripts and styles. | |
add_action( 'wp_head', 'dcwd_wp_head_ob_start', 0 ); | |
function dcwd_wp_head_ob_start() { | |
ob_start(); | |
} | |
add_action( 'wp_head', 'dcwd_wp_head_ob_end', 10000 ); | |
function dcwd_wp_head_ob_end() { | |
$wp_head_markup = ob_get_contents(); | |
ob_end_clean(); | |
// Remove the 'type' attribute. Note the use of single and double quotes. | |
$wp_head_markup = str_replace( " type='text/javascript'", '', $wp_head_markup ); | |
$wp_head_markup = str_replace( ' type="text/javascript"', '', $wp_head_markup ); | |
$wp_head_markup = str_replace( ' type="text/css"', '', $wp_head_markup ); | |
$wp_head_markup = str_replace( " type='text/css'", '', $wp_head_markup ); | |
echo $wp_head_markup; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment