{"id":393,"date":"2011-10-30T13:56:08","date_gmt":"2011-10-30T13:56:08","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=393"},"modified":"2011-10-30T13:56:16","modified_gmt":"2011-10-30T13:56:16","slug":"optimizing-site-mobile-devices","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/optimizing-site-mobile-devices\/","title":{"rendered":"Beyond optimizing your site for mobile devices"},"content":{"rendered":"<p>Optimizing your website for mobile devices is a great start to improving user retention on mobile devices, but are you forcing users into a mobile experience that they weren&#39;t expecting?<\/p>\n<p>Sure, it&#39;s common practice, generally in the website footer, to allow a user to switch back to the desktop version of a site, but a frequent user of your site will quickly become annoyed at the need to choose the desktop version each time they load your site on a mobile device, if that is their preference.<\/p>\n<p>By combining 3 scopes, <strong>request<\/strong>, <strong>session<\/strong> and <strong>cookies<\/strong>, you can create a robust solution for detecting the presence of a mobile browser and storing a user&#39;s preference. To break this process down into steps, which could be performed in a conditional if\/then\/else statement:<\/p>\n<ol>\n<li>Request. Check the query string for the presence of a preference variable, which indicates that the user wishes to switch to either the mobile or desktop version of the site. This is traditionally done with a link in the footer of a site, i.e.\n<p>\t\t<code>&lt;a href=&quot;?browser=mobile&quot;&gt;Mobile&lt;\/a&gt; <br \/>\n\t\t&lt;a href=&quot;?browser=desktop&quot;&gt;Desktop&lt;\/a&gt;<\/code><\/p>\n<p>\t\tIf this variable is present in the query string, store the preference in the session and cookie scopes.<br \/>\n\t\t&nbsp;<\/li>\n<li>Check the session scope for the presence of the preference variable.<\/li>\n<li>Check the cookie scope for the presence of the preference variable.<\/li>\n<li>Perform a user agent detection to determine if the user is on a mobile device.<\/li>\n<li>If all else fails, assume the user is viewing the site in a desktop browser.<\/li>\n<\/ol>\n<p>For many of us, seeing a code example is more helpful in illustrating a process, so here&#39;s a quick PHP example:<\/p>\n<p><code>if (isset($_REQUEST[&#39;browser&#39;])) {<br \/>\n\t&nbsp;&nbsp;&nbsp; if ($_REQUEST[&#39;browser&#39;] == &#39;desktop&#39; || $_REQUEST[&#39;browser&#39;] == &#39;mobile&#39;) {<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $browser = $_REQUEST[&#39;browser&#39;];<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_SESSION[&#39;browser&#39;] = $_REQUEST[&#39;browser&#39;];<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $_COOKIE[&#39;browser&#39;] = $_REQUEST[&#39;browser&#39;];<br \/>\n\t&nbsp;&nbsp;&nbsp; }<br \/>\n\t}<br \/>\n\telseif (isset($_SESSION[&#39;browser&#39;]))<br \/>\n\t&nbsp;&nbsp;&nbsp; $browser = $_SESSION[&#39;browser&#39;];<br \/>\n\telseif (isset($_COOKIE[&#39;browser&#39;]))<br \/>\n\t&nbsp;&nbsp;&nbsp; $browser = $_COOKIE[&#39;browser&#39;];<br \/>\n\telseif ( \/\/ mobile detection logic goes here)<br \/>\n\t{<br \/>\n\t&nbsp;&nbsp;&nbsp; $browser = &#39;mobile&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; $_SESSION[&#39;browser&#39;] = &#39;mobile&#39;;<br \/>\n\t}<br \/>\n\telse<br \/>\n\t&nbsp;&nbsp;&nbsp; $browser = &#39;desktop&#39;;<br \/>\n\t<\/code><\/p>\n<p>You can now safely assume that the <em>$browser<\/em> variable will contain the users most desired preference between the desktop and mobile version of your site.<\/p>\n<p>As a bonus, here&#39;s a useful PHP script that will determine the <a href=\"\/coda\/php\/check-presence-mobile-browser.html\">presence of a mobile browser<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optimizing your website for mobile devices is a great start to improving user retention on mobile devices, but are you forcing users into a mobile&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[132,16],"tags":[134],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/393"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":6,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/393\/revisions\/399"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}