Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / shortcode-function.php
Last active August 29, 2015 13:56
Example of how to implement a shortcode
<?php
/**
* Custom shortcode callback function
*
* @param array $atts
* @param string $content
* @return string
*/
function my_shortcode_callback( $atts = array(), $content = '' ) {
@wpscholar
wpscholar / array-keys-white-list.php
Created March 24, 2014 15:41
Filter an array based on a white list of keys
<?php
/**
* Filter an array based on a white list of keys
*
* @param array $array
* @param array $keys
* @return array
*/
function array_keys_white_list( array $array, array $keys ) {
@wpscholar
wpscholar / array-keys-black-list.php
Created March 24, 2014 15:42
Filter an array based on a black list of keys
<?php
/**
* Filter an array based on a black list of keys
*
* @param array $array
* @param array $keys
* @return array
*/
function array_keys_black_list( array $array, array $keys ) {
@wpscholar
wpscholar / set-timezone.php
Created March 31, 2014 20:50
Properly set the timezone in WordPress
<?php
/**
* Properly set the timezone in WordPress
*
* @param string $timezone
*/
public static function set_timezone( $timezone = '' ) {
if ( empty( $timezone ) ) {
$timezone = get_option( 'timezone_string', date_default_timezone_get() );
@wpscholar
wpscholar / load-views.php
Created July 23, 2014 18:47
Load Ember templates in WordPress
<?php
/**
* Load Ember templates in WordPress
*/
public static function load_views() {
$iterator = new \DirectoryIterator( plugin_dir_path( __FILE__ ) . 'views' );
foreach ( $iterator as $file ) {
/**
* @var \SplFileInfo $file
@wpscholar
wpscholar / sanitize-mime-type.php
Created August 9, 2014 12:51
Sanitize mime type example
<?php
$mime_type = sanitize_mime_type( $_FILES['upload']['type'] );
update_post_meta( get_the_ID(), 'mime_type', $mime_type );
@wpscholar
wpscholar / sanitize-url.php
Created August 9, 2014 12:52
Sanitize url example
<?php
$url = esc_url_raw( $_POST['url'] );
update_post_meta( get_the_ID(), 'url', $url );
@wpscholar
wpscholar / sanitize-email.php
Created August 9, 2014 12:53
Sanitize email example
<?php
$email = sanitize_email( $_POST['email'] );
update_post_meta( get_the_ID(), 'email', $email );
@wpscholar
wpscholar / sanitize-title.php
Last active August 29, 2015 14:05
Sanitize title example
<?php
$slug = sanitize_title( $_POST['title'], 'untitled' );
update_post_meta( get_the_ID(), 'slug', $slug );
@wpscholar
wpscholar / sanitize-text-field.php
Created August 9, 2014 13:02
Sanitize text field example
<?php
$title = sanitize_text_field( $_POST['title'] );
update_post_meta( get_the_ID(), 'title', $title );