WordPress wp_is_mobile() function not working

I have a plugin on my WordPress that I want to block when a user is on mobile. In my functions.php I added the line:

if (wp_is_mobile())
{
    wp_dequeue_script('flare');
    wp_deregister_script('flare');
}

Unfortunately, this made the script not load for both mobile and desktop users. So I need to figure out a way to make this script unload if they are on mobile.

Read More

I used a similar function inside my post-template, for adding regular share buttons at the bottom of the post if they were on mobile – but this also didn’t work. It added the share buttons for both mobile and desktop users.

Any help would be greatly appreciated!

Related posts

Leave a Reply

3 comments

  1. Try this…

    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
                        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
                        //Mobile browser do stuff here       
            } else {
                        //Do stuff here
                }
    
  2. Hello just addition to @jay bhatt ans please check your functions.php for the filter

    wp_is_mobile
    

    code may look like

    add_filter( 'wp_is_mobile', 'custom_detection_code', 99, 1 );
    

    then think for Jay bhatt’s answer

  3. I think Script dequeuing calls should be added to the wp_print_scripts action hook.

    Scripts are normally enqueued on the wp_enqueue_script hook, which happens early in the wp_head process. The wp_print_scripts hook happens right before scripts are printed. So I think you should be doing as follows :

    function deque_my_scripts () {
        wp_dequeue_script('flare');
        wp_deregister_script('flare');
    }
    
    if (wp_is_mobile())
    {
       add_action('wp_print_scripts','deque_my_scripts');
    }
    

    You can also add the action hook to wp_enqueue_scripts

    So another method will be

    if (wp_is_mobile())
    {
       add_action('wp_enqueue_scripts','deque_my_scripts', 20);
    
    }
    

    Hope this helps you 🙂