Skip to content

Instantly share code, notes, and snippets.

😀
Happy

Micah Wood wpscholar

😀
Happy
Block or report user

Report or block wpscholar

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@wpscholar
wpscholar / wp-error-merge.php
Last active Aug 29, 2015
Merge multiple WP_Error objects together
View wp-error-merge.php
<?php
/**
* Merge multiple WP_Error objects together
*
* @return WP_Error
*/
function wp_error_merge() {
$wp_error_merged = new WP_Error();
$wp_errors = func_get_args();
@wpscholar
wpscholar / shortcode-function.php
Last active Aug 29, 2015
Example of how to implement a shortcode
View shortcode-function.php
<?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 Mar 24, 2014
Filter an array based on a white list of keys
View array-keys-white-list.php
<?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 Mar 24, 2014
Filter an array based on a black list of keys
View array-keys-black-list.php
<?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 Mar 31, 2014
Properly set the timezone in WordPress
View set-timezone.php
<?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 Jul 23, 2014
Load Ember templates in WordPress
View load-views.php
<?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 / esc-js.php
Last active Aug 29, 2015
Escape JS example
View esc-js.php
<script>
var name = '<?php echo esc_js( $_POST['name'] ); ?>';
</script>
<a href="<?php echo esc_url( home_url( '/blog/' ) ); ?>"
onclick="<?php echo esc_js( 'alert("Welcome " + name);' ); ?>">
<?php _e( 'Blog', 'textdomain' ) ?>
</a>
@wpscholar
wpscholar / sanitize-html-class.php
Created Aug 9, 2014
Sanitize HTML class example
View sanitize-html-class.php
<?php
$html_class = sanitize_html_class( $_POST['html_class'] );
update_post_meta( get_the_ID(), 'html_class', $html_class );
@wpscholar
wpscholar / wpdb-update.php
Created Aug 9, 2014
$wpdb->update() example
View wpdb-update.php
<?php
global $wpdb;
$wpdb->update(
$wpdb->posts,
array( 'post_type' => 'new' ),
array( 'post_type' => 'old' ),
array( '%s' ),
array( '%s' )
);
@wpscholar
wpscholar / validate-safe-html.php
Created Aug 9, 2014
Validate safe HTML example
View validate-safe-html.php
<?php
$message = wp_kses_post( $_POST['message'] );
update_post_meta( get_the_ID(), 'message', $message );
You can’t perform that action at this time.