Categories
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;
}

Categories
Code

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']) );
}

Categories
Code

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 https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Adding-your-own-show_on-filters
*
* @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 );

Categories
Code

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;
    }
}
Categories
Code

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';
     }

     wp_reset_query();
     return $return_string;
}