I've followed a few tutorials on Dequeueing and Deregistering scripts and styles for WP - trying to optimise my site speed.
Initially I could dequeue and deregister, until I added a condition for mobile only sites. Now the functions won't work.
add_action( 'wp_enqueue_style', 'remove_default_stylesheet', PHP_INT_MAX);
if ( is_front_page() && wp_is_mobile() ) {
function remove_default_stylesheet() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
} elseif ( is_front_page() ) {
function remove_default_stylesheet() {
wp_dequeue_style( 'contact-form-7' );
wp_deregister_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
if ( is_front_page() && wp_is_mobile() ) {
function my_deregister_javascript() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
} elseif ( is_front_page() ) {
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
function my_deregister_javascript() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
}
I'm trying to dequeue and deregister scripts and styles depending on whether it's the homepage, and if the user is on mobile.
I can't understand why this isn't working despite searching about
I've followed a few tutorials on Dequeueing and Deregistering scripts and styles for WP - trying to optimise my site speed.
Initially I could dequeue and deregister, until I added a condition for mobile only sites. Now the functions won't work.
add_action( 'wp_enqueue_style', 'remove_default_stylesheet', PHP_INT_MAX);
if ( is_front_page() && wp_is_mobile() ) {
function remove_default_stylesheet() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
} elseif ( is_front_page() ) {
function remove_default_stylesheet() {
wp_dequeue_style( 'contact-form-7' );
wp_deregister_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
if ( is_front_page() && wp_is_mobile() ) {
function my_deregister_javascript() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
} elseif ( is_front_page() ) {
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX);
function my_deregister_javascript() {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
}
I'm trying to dequeue and deregister scripts and styles depending on whether it's the homepage, and if the user is on mobile.
I can't understand why this isn't working despite searching about
Share Improve this question edited Jul 29, 2019 at 23:31 PublicDisplayName asked Jul 29, 2019 at 22:35 PublicDisplayNamePublicDisplayName 1114 bronze badges1 Answer
Reset to default 1You should run the conditional tags (e.g. is_front_page()
) from inside the callback/function (e.g. remove_default_stylesheet()
) — see Where to Use Conditional Tags and the warning here. And there's no hook named wp_enqueue_style
; just use the wp_enqueue_scripts
to enqueue/register/dequeue/deregister a stylesheet file.
So I'm not sure the exact conditionals you need, but the following would dequeue/deregister the contact-form-7
style and script files on the front page and for mobile devices only:
add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', PHP_INT_MAX );
function remove_default_stylesheet() {
// Remove contact-form-7 on the front page and only for mobile devices.
if ( is_front_page() && wp_is_mobile() ) {
wp_dequeue_style( 'contact-form-7' );
wp_deregister_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript', PHP_INT_MAX );
function my_deregister_javascript() {
// Remove contact-form-7 on the front page and only for mobile devices.
if ( is_front_page() && wp_is_mobile() ) {
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
}
You can also combine the functions:
add_action( 'wp_enqueue_scripts', 'remove_plugin_scripts', PHP_INT_MAX );
function remove_plugin_scripts() {
// Remove contact-form-7 on the front page and only for mobile devices.
if ( is_front_page() && wp_is_mobile() ) {
// Remove style file.
wp_dequeue_style( 'contact-form-7' );
wp_deregister_style( 'contact-form-7' );
// Remove script file.
wp_dequeue_script( 'contact-form-7' );
wp_deregister_script( 'contact-form-7' );
}
}