Code Web Design

iOS prevent scrolling on body

Ordinarily, overflow: hidden; on the body tag is sufficient to prevent scrolling a web page, if for instance you’re creating a drawer to hold content that will scroll separately. However, this doesn’t work in iOS6. The best I’ve come up with so far is to set position: fixed:

body.lock-position {
height: 100%;
overflow: hidden;
width: 100%;
position: fixed;


Laravel UK date validator

Extend Laravel’s validator class to fully validate dates in the format (DD/MM/YY). Tested in laravel 3.

public function validate_uk_date($attribute, $value, $parameters)
Validates a UK date to fit DD/MM/YY, checking for days in the month and accounting for leap years.
Laravel's date validator didn't seem to do that.

//split date into array of parts
$date_array = strptime ( $value, '%d/%m/%y' );
//return early if that failed
if ( ! $date_array ) return false;
//check for validity of date
return checkdate( (1+$date_array['tm_mon']), $date_array['tm_mday'], (1900+$date_array['tm_year']) );


Custom Metaboxes for WordPress: Front Page show_on filter

Shows the metabox only if a static front page is set and you’re editing it.

Make sure your metabox is set thus:
'pages' => array( 'page' ), // Post
'show_on' => array( 'key' => 'front-page', 'value' => '' ),

* Include metabox on front page
* @author Ed Townend
* @link
* @param bool $display
* @param array $meta_box
* @return bool display metabox
function ed_metabox_include_front_page( $display, $meta_box ) {
if ( 'front-page' !== $meta_box['show_on']['key'] )
return $display;

// Get the current ID
if ( isset( $_GET['post'] ) ) {
$post_id = $_GET['post'];
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = $_POST['post_ID'];

//return false early if there is no ID
if( !isset( $post_id ) ) return false;

//Get ID of page set as front page, 0 if there isn't one
$front_page = get_option('page_on_front');

if ( $post_id == $front_page ) {
//there is a front page set and we're on it!
return $display;

add_filter( 'cmb_show_on', 'ed_metabox_include_front_page', 10, 2 );


Get WordPress attachment ID by URL

Pass in an (non-resized) attachment URL in WordPress and it returns the ID of the attachment. Doesn’t work with thumbnails.

function ed_get_attachment_id_by_url($url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE guid='%s';", $url));
    if ( $attachment ) {
        return $attachment[0];
    } else {
        return false;

WordPress Protect Content shortcode

The simplest way to hide content from logged out users by wrapping content in [protect] hide me [/protect]

PROTECT SHORTCODE - If user is not logged in, content within [protect] is replaced with a predefined string.

add_action( 'init', 'dm2_register_protect_shortcode');

function dm2_register_protect_shortcode() {
   add_shortcode('protect', 'dm2_protect_function');

function dm2_protect_function($atts, $content = null) {

     if ( is_user_logged_in() ) {
          $return_string = $content;
     } else {
          $return_string = 'You must be logged in to view this content';

     return $return_string;

Air Display uninstaller leaves kexts behind

While looking into the course of kernel panics on my iMac, I found that two kexts relating to Avatron’s Air Display were in /System/Library/Extensions, despite the fact that I uninstalled Air Display using their bundled uninstaller a bit back. The two offending files are AVVideoCard.kext and AVFrameBuffer.kext. Simply moving them outside of the Extensions folder and rebooting is enough to get rid of them, and there doesn’t seem to be any ill effects.

We’ll see if that fixes the kernel panics.


Categories images plugin modified to save image ID

I very quickly (and badly) hacked up Zahlan’s excellent Categories Images plugin to save image IDs rather than the absolute URL to the image file. This means we can use WordPress’ image resizing as well as its other image meta tools.

Download the categories-images plugin (zip)

Update (10 Oct 12): The first upload had a bit of code left in from my own use of the plugin that limited it to a certain taxonomy. I’ve removed it now so the plugin works on all taxonomies as it should.

The key points are:

  • z_edit_taxonomy_field() and z_add_taxonomy_field()  are modified to have a separate button to upload the image rather than clicking the text box. It means the user can manually type the ID if they wish.
  • z_script() grabs the image ID from thickbox instead of the URL. Because thickbox returns an img tag with the image, the only way to get the ID is to chop it out of the class.
  • z_save_taxonomy_image() validates the image ID before saving in case the user types an ID in the text box.

Add menu li class of item’s name in WordPress

This adds a class of the item’s name to each list item in a nav menu in WordPress. So, if the page item is called ‘About us’, the li gets an extra class of ‘menu-item-about-us’. Handy for styling.

add_filter('nav_menu_css_class' , 'ed_page_title_class' , 10 , 2);
function ed_page_title_class($classes, $item){
  $classes[] = sanitize_title('menu-item-' . $item->title);
  return $classes;

Add body class if post has a featured image in WordPress

This WordPress function adds a body class ‘has-featured-image’ if the current post has a featured image assigned.

add_action('body_class', 'ed_if_featured_image_class' );
function ed_if_featured_image_class($classes) {
 if ( has_post_thumbnail() ) {
 array_push($classes, 'has-featured-image');
 return $classes;

Fix Server Execution Failed message in Windows Media Player

Every now and then, when you try to open a media file, Windows Media Player does nothing for about 30 seconds and then comes up with this cryptic dialog box:
Server Execution Failed dialog box

Luckily it’s easy to fix:

  1. Open Command Prompt as Administrator (Click Start, Type cmd, right Click Command Prompt, select Run as Administrator)
  2. Type regsvr32 jscript.dll and press enter.
  3. A dialog will pop up. Click OK
  4. Now type regsvr32 vbscript.dll and press enter again.
  5. Click OK again
  6. Close Command Prompt and try opening the file again!