I have the following code in functions.php. The first function correctly enqueues the script based on the page name. However, the second function always adds the 'defer' attribute.
function custom_scripts() {
global $page;
if ($page->page_name !== 'awkward_page') {
wp_enqueue_script('jquery');
}
// ...
}
add_action( 'wp_enqueue_scripts', 'custom_scripts' );
function defer_scripts( $tag, $handle ) {
global $page;
if ($page->page_name !== 'awkward_page') {
return str_replace( ' src', ' defer src', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'defer_scripts', 10, 2 );
What is wrong with this code?
If this is the wrong way to conditionally add the 'defer' html attribute, what is the correct way?