-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-type-attr-html.php
82 lines (69 loc) · 2.4 KB
/
remove-type-attr-html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Add all the below mentioned code in your theme's functions.php
* to fix the W3Validator Warnings of `type` attribute in
* script and style elements.
*/
/**
* Enable Output buffering and attach the function on WordPress init
*/
function yasglobal_init_html() {
ob_start( 'yasglobal_page_html' );
}
add_action( 'init', 'yasglobal_init_html', 1 );
/**
* This function works at the end and removed the type attribute
* from the style and script tags.
*/
function yasglobal_page_html($buffer) {
$patterns = array();
$patterns[0] = 'type="text/javascript"';
$patterns[1] = "type='text/javascript'";
$patterns[2] = 'type="text/css"';
$patterns[3] = "type='text/css'";
$replacements = array();
$replacements[0] = 'type="text/javascript"#yasglobal_break_script#';
$replacements[1] = "type='text/javascript'#yasglobal_break_script#";
$replacements[2] = 'type="text/css"#yasglobal_break_style#';
$replacements[3] = "type='text/css'#yasglobal_break_style#";
$buffer = str_replace( $patterns, $replacements, $buffer );
$output = $buffer;
$buffer = '';
$break_point_patterns = array();
$break_point_patterns[0] = ' type="text/javascript"';
$break_point_patterns[1] = " type='text/javascript'";
$output_script = explode( '#yasglobal_break_script#', $output );
foreach ( $output_script as $row ) {
$type_attribute_script = explode( '<script', $row );
$type_attribute_noscript = explode( '<noscript', $row );
if ( isset( $type_attribute_script[1] )
|| isset( $type_attribute_noscript[1] ) ) {
$replaced_break_point = str_replace( $break_point_patterns, '', $row );
} else {
$replaced_break_point = $row;
}
$buffer .= $replaced_break_point;
}
$output = $buffer;
$break_point_patterns[0] = ' type="text/css"';
$break_point_patterns[1] = " type='text/css'";
$output_style = explode( '#yasglobal_break_style#', $output );
$buffer = '';
foreach ( $output_style as $row ) {
$type_attribute = explode( '<style', $row );
if ( isset($type_attribute[1]) ) {
$replaced_break_point = str_replace( $break_point_patterns, '', $row );
} else {
$replaced_break_point = $row;
}
$buffer .= $replaced_break_point;
}
return $buffer;
}
/**
* Send and turn off the buffering on WordPress shutdown hook
*/
function yasglobal_shutdown_html() {
ob_end_flush();
}
add_action( 'shutdown', 'yasglobal_shutdown_html', 1000 );