scottsweb / mobble

✅ WordPress plugin that provides conditional functions for detecting a variety of devices.
http://scott.ee/journal/mobble/
Other
85 stars 19 forks source link

Mobify the body class isn't working #8

Closed rpkoller closed 9 years ago

rpkoller commented 9 years ago

Hi I've tried the Mobify body class option. But unfortunately nothing is added. The body tag remains in its initial state. But the mobble plugin itself is working. A simple test with

if (is_mobile()) {
  print_r('It is a mobile device');
}

works flawlessly. The output shows on my smartphone while nothing is showing on my desktop. So only the adding of the body class is failing. I am running mobble 1.4 and WordPress 4.3 using a custom template with Timber 0.2.19. If you need any further infos let me know. Best regards r.

scottsweb commented 9 years ago

Can you check that your theme uses the body_class() function (normally in header.php)?

This could also be a caching issue. mobble does not work well on sites that are heavily cached - it will require your cache strategy to be aware of mobile devices too.

rpkoller commented 9 years ago

oh actually then it's an easy one. i've left out the body_class() function intentionally because it is cluttering the body tag with an unnecessary number of classes. Actually haven't thought about that mobble relies onto the body_class() function for placing the device related classes :/ hm guess there isn't a way to keep out the wordpress classes added by default and only add those related to mobble by the body_class() function?

scottsweb commented 9 years ago

guess there isn't a way to keep out the wordpress classes added by default and only add those related to mobble by the body_class() function?

There is. You would need to filter out existing classes that are added by WordPress (and other plugins). Something along the lines of:

add_filter( 'body_class', 'custom_body_class' );

function custom_body_class( $classes ) {
    $myclasses = array();
    $myclasses[] = "body-class-custom";
    // you could add more classes here from mobble: https://github.com/scottsweb/mobble/blob/master/mobble.php#L411-L449
    return $myclasses;
}