Extension:MobileDetect

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
MobileDetect

Release status: beta

Implementation Tag
Description Detects mobile devices and allows to control the content visible with the <nomobile> and <mobileonly> tags.
Author(s) Matthew Tran (Archivolt), Luis Felipe Schenone
Latest version 2.0 (2014-09-29)
MediaWiki 1.15+
License GPLv2+
Download
Tags
<nomobile>, <mobileonly>
Hooks used
ParserFirstCallInit

Universal

Translate the MobileDetect extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

This extension detects mobile devices using PHP's HTTP_USER_AGENT. Due to the nature of the extension, it should be very compatible with both new and old versions of MediaWiki.

The extension introduces a function called mobiledetect(), which returns true when a mobile device is detected, and false otherwise. It also introduces the <nomobile> and <mobileonly>, which allow users to control which content is displayed only in mobile browsers, and which content is displayed only in desktop browsers.

Installation[edit | edit source]

  • Download and extract the file(s) in a directory called MobileDetect in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository using:
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/MobileDetect.git
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/MobileDetect/MobileDetect.php";
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Usage[edit | edit source]

mobiledetect() function[edit | edit source]

If you would like to set a default theme depending on a visitor's browser, you can add this to your LocalSettings.php:

require_once "$IP/extensions/MobileDetect/MobileDetect.php";
$mobile = mobiledetect();
if ( $mobile == false ) $wgDefaultSkin = "vector"; # If not Mobile
if ( $mobile == true ) $wgDefaultSkin = "chick"; # If is Mobile

With this code, if the visitor's browser shows a user agent from a mobile browser, the default theme will be "chick" instead of "vector". If instead the browser shows a user agent from a desktop/full browser, the default theme will be "vector" instead of "chick".

If you would like a certain extension to be excluded from loading on mobile browsers, you can add this to your LocalSettings.php:

require_once "$IP/extensions/MobileDetect/MobileDetect.php";
$mobile = mobiledetect();
if ( $mobile == false ) require_once "$IP/extensions/reCAPTCHA/recaptcha.php"; # Only load if desktop browser

With this code, the reCAPTCHA extension would only load on desktop/full browsers.

<nomobile> and <mobileonly> tags[edit | edit source]

The MobileDetect extension also introduces the <mobileonly> and <nomobile> tags, which allow users to control which content is displayed in mobile browsers, and which in desktop/full browsers. Whatever is wrapped between <mobileonly> tags will only be displayed in mobile browsers, and whatever is wrapped between <nomobile> tags will only be displayed in desktop/full browsers. So for example:

<mobileonly>This will not be displayed in desktop/full browsers, only in mobile browsers</mobileonly>
<nomobile>This will not be displayed in mobile browsers, only in desktop/full browsers</nomobile>

The naming and behaviour of the <mobileonly> and <nomobile> tags follows that of the <includeonly> and <noinclude> tags.

See also[edit | edit source]