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 would like to add http://mobiledetect.net/ class to my template. Which is the best approach to dynamically use this class in every part(s) of my template?

For the moment I just loaded the class in functions.php like this

    function devicecontrol() {
    require_once '_/inc/md/Mobile_Detect.php';
}
add_action('wp_loaded', 'devicecontrol', '1');

but to use it, I must first create an instance in every page-template

$detect = new Mobile_Detect;

I know global variables are not a good solution, which is then the best approach to include such classes?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

global variables are not a good solution

Yes, I heard this sentence a lot of times, but as you experienced, sometimes some variables need to be accessed everywhere.

Some modern PHP frameworks implement advanced patterns like IOC that helps in such type of things, but wordpress lacks in that, and this is the reason a lot of global variables is used in WP.

Generally speaking, in PHP, solutions are:

  1. Sigleton. A lot of coder (specially from other languages than PHP) says singleton sucks because are a sort of masked globals. In my opinion, when you can't rely in an implementation of IOC, singleton are better than 'pure' globals, but for simple tasks is not necessary implement this pattern.
  2. Globals. In PHP the keywords global let global vars smell a little less. If you make a thoughtful use, globals doesn't suck so much. E.g. using them inside functions is not a very bad solution.

Strictly related your case, a static method will suffice as alternative to globals, if you just hate them.

class MyDetect {

  static $detect = NULL;
  static $deviceType = NULL;

  static function detect() {
    if ( is_null(self::$detect) ) {
      require_once '_/inc/md/Mobile_Detect.php';
      self::$detect = new Mobile_Detect;
      self::$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); 
    }
  }

}

Require the file containing the class in your plugin, then

add_action('wp_loaded', array('MyDetect', 'detect') );

and when you need the $detect variable (the mobile detect instance) use MyDetect::$detect and when you need the $deviceType variable use MyDetect::$devicType

Edit: an example of usage

function add_mobile_scripts() {
   wp_enqueue_script('my_script_for_mobiles', 'the_script_url_here' );
}

function add_phone_scripts() {
   wp_enqueue_script('my_script_for_phones', 'the_script_url_here' );
}

function add_tablet_scripts() {
   wp_enqueue_script('my_script_for_tablets', 'the_script_url_here' );
}

function add_desktop_scripts() {
   wp_enqueue_script('my_script_for_desktops', 'the_script_url_here' );
}

function addDevicesScripts() { 
  if ( MyDetect::$deviceType == 'phone' || MyDetect::$detect == 'tablet' ) {
    add_mobile_scripts();
    if ( MyDetect::$deviceType=='phone' ) {
      add_phone_scripts();
    } else {
      add_tablet_scripts();
    }  
  } else {
     add_desktop_scripts();
  }
}

add_action( 'wp_enqueue_scripts', 'addDevicesScripts' );
share|improve this answer
    
wow thank you! If I understand your code you are just calling the function every time you need it without storing its value –  Andrea Puiatti Aug 21 '13 at 16:34
    
Not really. I call the detect method once, on wp_loaded and when it's called, a Mobile_Detect instance is stored as a static variable of MyDetect class. 'Static' means that variable is instanziate only once and its value is kept when the methods ends for the rest of the script life. Then everytime you need the variable instead of search it in the global space you search in the local space of MyDetect class. Find here info on PHP static variable (and variable scope in general) @AndreaPuiatti –  G. M. Aug 21 '13 at 16:58
    
@AndreaPuiatti, I've edited a little bit the class code and added an example of usage –  G. M. Aug 21 '13 at 19:24
    
Hey thank you Giuseppe that was great! In this specific case do you think that is better to include the MyDetect class in a separate plugin or directly into the funcions.php ? –  Andrea Puiatti Aug 24 '13 at 10:45
    
You're welcome. It depends on your application structure. If you mobile detection stuff only to embed scripts in theme and you are not planning to distribuite you code, put class in a separate file, in a folder with the Mobile_Detect.php file and require from function.php. It will make it reusable and keep your code clean. @AndreaPuiatti –  G. M. Aug 24 '13 at 12:58

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.