title: Simple Android versus iOS detection in WordPress theme
body: <p>I am trying to edit my WordPress theme file to show one ad code if an Android device is detected, another set of code if iOS.</p>
  
  <p>The code is in the format:</p>
  
  <pre><code>&lt;script type="text/javascript" src="//xxxxxxxx.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>I have tried several previous answers, such as the following:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/21741841/detecting-ios-android-operating-system">Detecting iOS / Android Operating system</a></li>
  <li><a href="https://stackoverflow.com/questions/6031412/detect-android-phone-via-javascript-jquery">Detect Android phone via Javascript / jQuery</a></li>
  <li><a href="https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device-in-jquery">What is the best way to detect a mobile device in jQuery?</a></li>
  </ul>
  
  <p>All of which are slightly different and cause my page to not load (blank page).</p>
  
  <p>Could someone please give me a hand and post the full simplest/fastest code to simply detect Android versus iOS and do nothing for anything else (i.e. a Windows PC). Something I can just copy and paste into the theme files where I want it.</p>
  
  <p><strong>Edit:</strong> I also tried things like the following to no avail (this was exactly what I pasted into the WordPress theme file, without the <code>xxx</code> and including proper reference to external <em>.js</em> file:</p>
  
  <pre><code>&lt;?php var isAndroid = /(android)/i.test(navigator.userAgent);
  if (isAndroid) {?&gt;
  &lt;script type="text/javascript" src="xxxxxxx.js"&gt;&lt;/script&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;?php function getMobileOperatingSystem() {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;
    if( userAgent.match( /Android/i ) )
    {?&gt;
      &lt;script type="text/javascript" src="//xxxxxxx.js"&gt;&lt;/script&gt;
    &lt;?php}
  } ?&gt;
  </code></pre>