I am trying to enable a plugin only for mobile and I have consulted this thread: Deactivate plugins only for mobile devices
I have made some changes to the last function as I want to enable only for mobile, while the thread suggest how to disable to mobile.
I don't know how specify the plugin that I want to specify, so I include 2 files. This is in a folder in my wp-content/ plugins/name-of-plugin, where there are an index.php file and a name-of-plugin.php file.
This is my code, saved in a my-code.php file, places on mu-plugins of wordpress. Unfrotunately, it doesn't work. Any help how to fix this?
<?php
/**
* Plugin Name: My mobile enabler
*/
function my_non_mobile_plugins() {
return array(
// i am not sure how specifiy the plugin I want to disable. I have added these two lines
'/name-of-folder-plugin/plugin-name.php',
'/name-of-folder-plugin/index.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() ) {
$not_allowed = my_non_mobile_plugins(); // get non allowed plugins
return array_values( array_diff( $plugins, $not_allowed ) );
}
return $plugins; // for mobile device do nothing
}