Skip to content

Instantly share code, notes, and snippets.

View jimboobrien's full-sized avatar

Jimobrien jimboobrien

View GitHub Profile
@jimboobrien
jimboobrien / wp-get-excerpt-by-id.php
Created September 20, 2017 23:18 — forked from wpscholar/wp-get-excerpt-by-id.php
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;
@jimboobrien
jimboobrien / shortcode.php
Created September 20, 2017 23:17 — forked from wpscholar/shortcode.php
A class for creating shortcodes in a flexible way.
<?php
/**
* Class My_Shortcode
*/
class My_Shortcode {
/**
* The shortcode attributes
*
@jimboobrien
jimboobrien / multisite-migrate.php
Created September 20, 2017 23:16 — forked from wpscholar/multisite-migrate.php
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.
*/
@jimboobrien
jimboobrien / attachment-query.php
Created September 20, 2017 23:15 — forked from wpscholar/attachment-query.php
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',
@jimboobrien
jimboobrien / salt.sh
Created September 20, 2017 23:15 — forked from wpscholar/salt.sh
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
@jimboobrien
jimboobrien / custom-page-templates.php
Created September 20, 2017 23:15 — forked from wpscholar/custom-page-templates.php
Load custom page templates from within a plugin
<?php
/**
* Class Custom_Page_Templates
*/
class Custom_Page_Templates {
/**
* Storage for all custom template paths
*
@jimboobrien
jimboobrien / create-admin-user.php
Created September 20, 2017 22:44 — forked from wpscholar/create-admin-user.php
Create a new admin user in WordPress via code. Drop this file in the mu-plugins directory and update the variables, then load a page in WordPress to create the user. Remove the file when done.
<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = '[email protected]';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
@jimboobrien
jimboobrien / remote-media-loader.php
Created September 20, 2017 22:44 — forked from wpscholar/remote-media-loader.php
Load media from a remote site so you don't have to download the uploads directory.
<?php
/**
* Loads media from a remote site when on a local dev environment.
* Eliminates the need to download the uploads directory from the remote site for testing purposes.
*/
if ( 'mydomain.dev' === $_SERVER['HTTP_HOST'] ):
add_filter( 'upload_dir', function ( $uploads ) {
$uploads['baseurl'] = 'http://mydomain.com/wp-content/uploads';
@jimboobrien
jimboobrien / cookies.js
Created September 20, 2017 22:42 — forked from wpscholar/cookies.js
Functions for setting, getting and deleting cookies via JavaScript
/**
* Set a cookie
*
* @param {string} cookieName
* @param {string} cookieValue
* @param {object} cookieOptions (expires, domain, path)
*/
function setCookie(cookieName, cookieValue, cookieOptions) {
cookieOptions = cookieOptions || {};
if (cookieValue) {