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?

1 Answer 1


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' ) {
    if ( MyDetect::$deviceType=='phone' ) {
    } else {
  } else {

add_action( 'wp_enqueue_scripts', 'addDevicesScripts' );
  • wow thank you! If I understand your code you are just calling the function every time you need it without storing its value Aug 21, 2013 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
    – gmazzap
    Aug 21, 2013 at 16:58
  • @AndreaPuiatti, I've edited a little bit the class code and added an example of usage
    – gmazzap
    Aug 21, 2013 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 ? Aug 24, 2013 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
    – gmazzap
    Aug 24, 2013 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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