Hot answers tagged

31

In very layman term wp_is_mobile() is not for styling your theme. How it works: It matches some of device's native name in User Agent String. So if someone manipulate the string and send false information you can not detect what device it is. And it does not return any device name it just return true if you are on not on desktop else false How WordPress ...


16

Yes it works well. It's a very simple function but never found a mobile device not recognized by it. It recognize the 90%+ of mobile devices. Main difference from mobiledetect.net is that doesn't differe from phone and tablets. See the code


15

TL;DR Yes, WordPress can certainly act as a backend for a mobile app. Yes, a page can act as a rest endpoint / interface. No, a theme template is not the right territory for the logic. Write your own plugin. Pointers I find it hard to believe that no one else has done this. I, for one, have. More than once. And I'm near certain I'm not alone. "No one ...


13

t f's answer got me thinking. Actually, I can use the core function and adapt it as I like but just put everything in a new function. So here goes: function my_wp_is_mobile() { static $is_mobile; if ( isset($is_mobile) ) return $is_mobile; if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $is_mobile = false; } elseif ( ...


11

This is thanks to EAMann's brilliant answer above, and I thought it might be helpful to the original poster too... I needed a solution to make sure users deactivated my plugin if they uploaded the premium version (to avoid potential conflicts). Previously I detected its state with is_plugin_active and showed an admin error message to prompt users to switch ...


10

Yes, the wp_is_mobile() works well, but you can run into problems when using aggressive caching systems, such as fastcgi, proxy cache, etc. that skip the php execution. If that is the case, I would recomend some client side (javascript) detection method that is loaded for both mobile and desktop browsers.


8

It is a shitty idea to use that function. Device identification should always be done at the client side, and user agent is probably the worst way to identify whether a device supports a feature X or have a form factor Y. You start using it when the site is young and then your site grows and you decide to cache your HTML and boom all your device detection ...


7

I do think that the days of creating a separate theme for mobile users are long gone. With proper planning and layout you can use one theme on desktops, laptops, tablets and mobile devices I would tackle this is a much different way. Firstly, I would create my theme in such a way it is responsive on according to the required screensize like normal. For ...


7

You could accomplish this with a rewrite endpoint and a template filter. First, register a new rewrite endpoint for the page post type: function wpd_app_view_rewrite_endpoint() { add_rewrite_endpoint( 'app-view', EP_PAGES ); } add_action( 'init', 'wpd_app_view_rewrite_endpoint' ); Don't forget to flush rewrite rules after adding this (or just visit ...


6

Ok, you want that mobile devices always load mobile templates. Desktop devices load template files based on resolution: if < 1080 mobile ones, > 1080 desktop ones. Your workflow should be: On init you check for mobile devices using wp_is_mobile. If true you add a template filter that returns str_replace('.php', '-mobile.php', $template); where $template ...


6

I had exactly the same problem. In the base TwentyTwelve theme there has been a code change so that the site navigation changed from h3 to button. You need to update header.php file in the child theme to use <button class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></button> See WordPress TRAC #28824 comment 13 The problem ...


5

You can use the built in function wp_is_mobile() Override a style with wp_enqueue_style add_action('wp_enqueue_scripts', 'my_mobile_styles'); function my_mobile_styles(){ if(wp_is_mobile()){ wp_register_style( 'my-mobile-style', 'URL_to_stylesheet', array('id-of-main-stylesheet') ); wp_enqueue_style( 'my-mobile-style' ); } } Switch ...


5

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. ...


5

The activated theme is stored in the options table: template is the parent theme, stylesheet is the child theme. If there is no child theme the two values will be the same. The current hostname (URL without protocol or path) is available in the $_SERVER variable. You can then hook into the stylesheet and template filters to force a different theme. ...


5

Create 2 menus. One for display on mobiles and one as a default for desktops. Add this code in your header.php or you could use a hook in your functions file. <?php if ( wp_is_mobile() ) { wp_nav_menu( array( 'theme_location' => 'mobile-menu' ) ); } else { wp_nav_menu( array( 'theme_location' => 'desktop-menu' ) ); } ?> Or you could ...


5

As recommended in a similar post: https://wordpress.stackexchange.com/a/156494/74343 1.) Create the menus as you want them, and name them as you like, as an example "mobile-menu" and "desktop-menu". 2.) In your child theme in the header.php you could switch according to the wp_is_mobile() flag like this: if ( wp_is_mobile() ) { wp_nav_menu( array( '...


4

You could also use the regularly updated Mobile Detect PHP class to create a custom function for detecting mobiles excluding tablets (thus iPads). At time of writing this answer, the Github repo had most recently been updated to include detection for new Samsung tablets as of 3 months ago. Assuming you place the required file in directory called /includes/ ...


4

Hook into wp_head in your functions.php file. add_action('wp_head', 'add_your_stuff'); function add_your_stuff() { ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri();?>/favicon.ico" type="image/x-icon" /> <link rel="apple-touch-icon" href="<?php echo get_stylesheet_directory_uri();?>/apple-touch-...


4

If you deactivate a plugin, the change is stored in database, so in all subsequent access the plugin is deactivated, no matter the device used to access the site. Filtering the option Active plugin in WordPress are retrieved using wp_get_active_and_valid_plugins() function. It uses the option "active_plugins" to get the currently active plugin. All ...


3

Add a class .hide with property display: none; - specify only for mobile viewports, not in desktop styles assign the class to your widget or to the entire sidebar, as needed


3

Just wrap you code up in a conditional: if ( $GLOBALS['is_iphone'] ) { // do funky stuff for mini screens } global $is_iphone; will trigger TRUE for all mobile devices incl. tablets. Edit for WP 3.4+ Now there's wp_is_mobile() to make checks for User-Agent. It's basically a wrapper for $is_iphone and does the same.


3

I was able to resolve this issue without using a separate theme or subdomain. I have elements of my theme that are loaded/not loaded on the server side based on the user agent (mobile/not mobile). After installing and enabling some basic caching with W3 Total Cache, I was getting desktop elements on mobile or vice versa. In W3 Total Cache User Agent Groups ...


3

Simple: Use the wp_is_mobile() to test - it will trigger true for all mobile devices (smart phones, tablets, etc.). Update Please do not do that. It does not work reliable.


3

While s_ha_dum's answer is not only not wrong, but exactly the answer you'd expect to get, I'd highly advise against using it, whether you are on 3.4+ or not. User agent sniffing sucks. Whether done manually or via a core function. Period. For one, new UA strings pop up like wildfire. For another, a good number of browsers pretend to be something else ...


3

Personally I always tend to stick to using responsive design rather than a completely different theme. You then will only have theme to keep updated. Check out this article for more information http://www.techrepublic.com/blog/webmaster/how-to-get-started-with-responsive-web-design/1769


3

Yes. Using CSS won't prevent JS from loading generally but it's more reliable than server side sniffing. Depends on project but I would go with media queries to just hide it or bring it in page's content flow. @media (max-width: 768px) { .sticky-social { display: none; } }


3

There is wp_is_mobile() function to conditionally display (or block) content for mobile users. <?php if (!wp_is_mobile()) : ?> <!-- Stuff to hide from mobile users --> <?php endif; ?>


3

For the record, I'm not sure why anyone would use the JetPack JSON API as it currently works for a self hosted site. If you're doing AJAX and JSON, the whole point is to get back some JSON data FAST. Using jetpack involves an unnecessary call to wordpress.com, which then does a call to your own self hosted blog. Using the wonderful JSON API plugin, you ...


3

Firstly, you need to detect mobile devices, probably the easiest and a built in possibility is wp_is_mobile. It is simpler then other solutions that are available, but works reasonably well. It can be altered, if necessary, take a look at this question for a first insight into that. Secondly, if that concerns your main query, like your code suggests, you ...


3

You can have a subdomain, but you cannot detect mobile users reliably. And you shouldn’t. How to use a subdomain with the same content In your wp-config.php, look at $_SERVER['HTTP_HOST']. If it matches m.example.com, enforce the current domain as main domain with … const DOMAIN_CURRENT_SITE = 'm.example.com'; … and filter the active theme: add_filter( ...


Only top voted, non community-wiki answers of a minimum length are eligible