Reputation: 2293
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.
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!
Upvotes: 2
Views: 11958
Reputation: 258
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
Upvotes: 0
Reputation: 6335
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 :-)
Upvotes: -2
Reputation: 5651
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
}
Upvotes: 3