Detecting iPad

Need to tell if a user is browsing your site on an iPad? Here’s how to do so via PHP: <?php define(‘IS_IPAD’, (bool)strpos($_SERVER[‘HTTP_USER_AGENT’],’iPad’)); ?> One can also do it via Javascript: <script> var IS_IPAD = navigator.userAgent.match(/iPad/i) != null; </script> However, I prefer making the determination server-side, as there’s a (small) number of users who browse […]

Using GLOB to retrieve the filtered contents of a directory

I am not a demanding man. I have a directory of images; I want a list of the images inside. The usual drill is to create a directory handle and loop through, returning everything that’s found, then close the handle, like so: <?php $dir = “/var/www/vhosts/”; $handle = opendir($dir); while (false !== ($file = readdir($handle))) […]

Strip all non-alphanumeric characters from a string

Here’s a very handy little snippet for stripping all non-alphanumeric characters from a string: $string = preg_replace(“/[^A-Za-z0-9]/”, “”, $string); This will leave all letters (whether they are capitalized or lowercase) as well as numbers 0 through 9.

Using “OR” conditions in CakePHP

Qualifying a query in CakePHP is as simple as adding conditions to the conveniently-named “conditions” array, like so: // return array of all musicians $all = $this->Musician->find(‘all’); // return array of all available reed players $only_available_reeds = $this->Musician->find(‘all’, array( ‘conditions’ => array( ‘category’ => ‘reeds’, ‘status’ => ‘available’ ) )); If the array contains multiple […]

Finding the current theme directory in WordPress

WordPress is, for the most part, pretty intuitive. However, one place where WordPress complicates things unnecessarily is in finding the current theme directory. That’s not to say there aren’t intuitive ways of obtaining this information already built into WordPress: Need the current theme URL? Use get_template_directory_uri(). Need the absolute path to the current theme directory? […]