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 / wp-get-excerpt-by-id.php
Created February 17, 2014 20:46
Get the excerpt for a WP_Post by post ID.
<?php
/**
* Get the excerpt for a WP_Post by post ID.
*
* @param int $post_id
*/
function get_excerpt_by_id( $post_id = 0 ) {
global $post;
$save_post = $post;
@wpscholar
wpscholar / shortcode.php
Created February 20, 2014 22:29
A class for creating shortcodes in a flexible way.
<?php
/**
* Class My_Shortcode
*/
class My_Shortcode {
/**
* The shortcode attributes
*
@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 / multisite-migrate.php
Last active September 20, 2017 23:16
WP-CLI script for moving a multi-site instance from one domain to another
<?php
/**
* WP-CLI script for moving a multi-site instance from one domain to another
*
* Example command usage: wp eval-file multisite-migrate.php old-domain.com new-domain.com
* Note: Currently, this script doesn't update all domain references, such as in post content.
* At this time, it is primarily used when creating a local copy of a multi-site instance in
* order to ensure everything will load on a local dev domain.
*/
@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 / attachment-query.php
Created May 12, 2014 13:22
Practical example of how and why you might want to extend the WP_Query class.
<?php
/**
* Class Attachment_Query
*/
class Attachment_Query extends WP_Query {
function __construct( $query = '' ) {
$defaults = array(
'post_type' => 'attachment',
@wpscholar
wpscholar / salt.sh
Last active September 20, 2017 23:15
Create a new salt.php file with secret keys
echo "<?php" > salt.php
wget https://api.wordpress.org/secret-key/1.1/salt/ -O - >> salt.php
@wpscholar
wpscholar / custom-page-templates.php
Created July 1, 2014 18:05
Load custom page templates from within a plugin
<?php
/**
* Class Custom_Page_Templates
*/
class Custom_Page_Templates {
/**
* Storage for all custom template paths
*