Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the plugin "Fancybox for Wordpress" in WP 3.5.1. On small screens, I want to disable the fancybox-script. So, at top of header-file, I include a check for user agent, and sets global variable $is_mobile true or false. This works.

However, depending on this, I would like to add something like this to functions.php to disable the script on mobile devices.

$is_mobile;
function is_mobile() {
if ($is_mobile==true) {
    remove_action('wp_print_scripts', 'mfbfw_load');
    remove_action('wp_print_styles', 'mfbfw_css');
    remove_action('wp_head', 'mfbfw_init');

} else {$is_mobile = "false"; }

return $is_mobile;
}

add_action('wp_head', 'is_mobile', 1);

This doesn't work, maybe because functions.php is loaded before header.php. However, it also doesn't work if I include the check for useragent into the function.

Anyway, the variable $is_mobile is not recognized as a global in header.php.

share|improve this question
add comment

2 Answers

You could have a try with wp_is_mobile():

if ( wp_is_mobile() ) {
  remove_action( 'wp_print_scripts', 'mfbfw_load' );
  remove_action( 'wp_print_styles', 'mfbfw_css' );
  remove_action( 'wp_head', 'mfbfw_init' );
}

You should also check with tablets since they might have a big resolution and hence not fall in your "small screen" condition. You should note that the User Agent string can be modified by any browser sending the request.

share|improve this answer
add comment

Directly under that script try:

$is_mobile = is_mobile();

share|improve this answer
    
Please add an explanation to your answer: why could that solve the problem? –  toscho May 28 '13 at 10:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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