4

I tried to use deactivate_plugins function to deactivate a plugin only for mobile devices.

It worked: when I enter site on mobile phone the plugin is deactivated, but when I enter site on PC the plugin is deactivated too.

Is there a way to not deactivate a plugin, but only disable it for mobiles?

function deactivate_plugin_conditional() {
  if ( wp_is_mobile() ) {
    deactivate_plugins( array('/first-plugin/first.php', '/second-plugin/second.php') );    
  }
}

add_action( 'admin_init', 'deactivate_plugin_conditional' );
  • 1
    Plugins run on the server itself, not on the browser. If you deactivate a plugin, then that deactivates it, period. It turns it off. You shouldn't be turning off a plugin just because somebody visited the site from a mobile device. – Otto Oct 19 '14 at 1:59
  • Yea but i have a plugin that mess up all the mobile theme but it's working on pc. So i was thinking about disable it on mobiles only. So there is no way to do it so ? – c64girl Oct 19 '14 at 2:01
  • Although you cannot deactivate plugin for browser/client specific but you can modify the plugin behavior for clients. You will need to find the root of the error of your problem. It could be a JS or CSS. – Robert hue Oct 19 '14 at 2:48
3

If you deactivate a plugin, the change is stored in database, so in all subsequent access the plugin is deactivated, no matter the device used to access the site.

Filtering the option

Active plugin in WordPress are retrieved using wp_get_active_and_valid_plugins() function.

It uses the option "active_plugins" to get the currently active plugin. All options in WordPress can be filtered using the "option_{$option}" filter hook, that in this case is "option_active_plugins".

Acting early

You may think to use a plugin to filter that option, but the problem is that is possible that the plugins(s) you want to exclude are loaded before your plugin, because you can't never be sure you plugin is loaded for first.

The solution is to use a MU plugin for the scope.

Too early issue

MU plugins run very early in WordPress, that gives you the possibity to filter the "option_active_plugins" option.

Another issue is that MU plugins load very early, even before the file wp-includes/vars.php that contain the function wp_is_mobile() is loaded, so if you use that function inside a MU plugin you get a fatal error.

Load the file "manually" is not an option, because WordPress will load that file using require and not require_once, so if you load the file, when WordPress load it again you get a fatal error for function already defined.

The only solution is write again wp_is_mobile(), just coping from core and using a different name.

Let's code

Create PHP file and save in wp-content/mu-plugins folder.

First of all write there a function that returns all the plugin you want to disable for mobile devices:

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'

  );
}

After that write a clone of wp_is_mobile() function, with another name:

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

Finally, filter 'option_active_plugins' making use of previous function to check if remove plugins or not

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

}
  • It don't matter what the file name will be ? Or it have to be something like load.php ? I want to thank You very much worked like a charm. Thank You for step by step explanation of code writing, It helped a lot. – c64girl Oct 19 '14 at 13:33
  • 1
    No, file name is not important because WP loads all the MU plugins no matter the name. I suggest to not use too generic name like load.php and use instead more explicit name like no-mobile-plugins.php in this way it is immediately clear what that file does. E.g. Someone else may need to access to your files or the future you may forget what that load.php does... @c64girl – gmazzap Oct 19 '14 at 15:24
  • Ok if i can change it i will change it to no-mobile-plugins :) Thats why i was asking about the file name :) Thank You again for Your help. And explaining step by step thro the code. – c64girl Oct 19 '14 at 15:33
-2

The way I did it was to simply hide the plugin bits (this works if the plugin is used to display extra information, for instance social media tabs).

I simply looked with the Developer Tool in Chrome to see which was the div I wanted to hide, then in my own CSS I added a rule like this:

@media screen and (max-width: 767px) {
   div#nameOfDiv {
       display: none;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.