functions - Dequeue Scripts and Style for Mobile not working?

I've followed a few tutorials on Dequeueing and Deregistering scripts and styles for WP - trying to optimise my sit

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' );
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745286003a4620564.html

相关推荐

  • functions - Dequeue Scripts and Style for Mobile not working?

    I've followed a few tutorials on Dequeueing and Deregistering scripts and styles for WP - trying to optimise my sit

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信