最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Dequeue Scripts and Style for Mobile not working?

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

You 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' );
    }
}
发布评论

评论列表(0)

  1. 暂无评论