0

I try to not load a js script on mobile and I use a cdn and page cache on my wordpress siteweb.

I tryed this to unload the plugin in a php file in mu plugin folder but it didn't work, js is still load on mobile :

function my_non_mobile_plugins() {
  return array(

    // an array of all the plugins you want to exclude for mobile

    'plugin-folder/plugin-file.php',
    'another-plugin-folder/another-plugin-file.php',
    'no-folder-plugin-file.php'

  );
}

function my_is_mobile() { 
  $is_mobile = false;
  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
  ) {
    $is_mobile = true;
  }
  return $is_mobile;
}

add_filter( 'option_active_plugins', 'my_disable_plugins_for_mobiles' );

function my_disable_plugins_for_mobiles( $plugins ) {

  if ( ! my_is_mobile() ) {
    return $plugins; // for non-mobile device do nothing
  }

  $not_allowed = my_non_mobile_plugins(); // get non allowed plugins

  return array_values( array_diff( $plugins, $not_allowed ) );

}

I found this to add in function.php in an old post but I don't think it would work : maybe if I replace ( ! wp_is_mobile() ) by ( wp_is_mobile() ) ?

function custom_load_scripts() { 
   // Load if not mobile 
   if ( ! wp_is_mobile() ) { 
      // Example script 
      wp_enqueue_script( 'script-name',get_template_directory_uri() . '/js/example.js', array (), '1.0.0', true ); 
      } 
}

add_action( 'wp_enqueue_scripts', 'custom_load_scripts' );

Thnak you so much if you have a solution !

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
  • The function name is `my_is_mobile`. – Thomas Sablik Feb 14 '21 at 11:44
  • Looks like you just need to change your mobile detection logic, like [this](https://stackoverflow.com/questions/6322112/check-if-php-page-is-accessed-from-an-ios-device#6322131). – Pranav Kasetti Feb 14 '21 at 11:48
  • @PranavKasetti What's wrong with this detection logic? _"Looks like you **just** need to change your mobile detection logic"_ How could `if ( ! wp_is_mobile() ) { ` work if there doesn't exist a function `wp_is_mobile`? – Thomas Sablik Feb 14 '21 at 11:51
  • Missing iPhone, iPad, iPod – Pranav Kasetti Feb 14 '21 at 11:52
  • @PranavKasetti So you recommend to replace the logic instead of adding 3 values? Wouldn't it be better to describe it in your comment instead of posting a link? – Thomas Sablik Feb 14 '21 at 11:55
  • I think `wp_is_mobile` is included in Wordpress, so you could just use that. No need to redefine it. – Pranav Kasetti Feb 14 '21 at 11:58
  • @PranavKasetti The logic you recommended to replace is in OPs own function `my_is_mobile`, not included in Wordpress. If OP wants to use the included function `wp_is_mobile` how would it be possible to change the logic? – Thomas Sablik Feb 14 '21 at 12:04
  • You have misunderstood @ThomasSablik. He has tried two techniques. With his own detection technique he needs to add more cases. In the other case he may have not included Wordpress or that function may no longer work for all mobile devices. So my recommendation was to change his own detection technique. – Pranav Kasetti Feb 14 '21 at 12:11
  • Haha, looks like you just need to read the question. – Pranav Kasetti Feb 14 '21 at 12:52
  • @PravanaKasetti For me the question is pretty clear but I'm not interested in a discussion. OP found a custom function and tried to call it with a different name. Nowhere in the question it's mentioned that these are two different unrelated approaches. So I'm still sure that your comment isn't helpful. In either case the question should be closed. It's either a duplicate or unclear. – Thomas Sablik Feb 14 '21 at 15:30
  • Yes it seems that wordpress use wp_is_mobile to detect mobiles. I'm not a developer but try to understand the most thing I can and do by myself. Do you think the 2 second solution I wrote could work if I replace (! wp_is_mobile) by (wp_is_mobile) ? I tryed the 1rst solution but not this second yet. Not sure it's logical because it asks to load something (if I understand well) while I want unload js on mobile. – Vivons Soi Julia Feb 14 '21 at 19:12

0 Answers0