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-redactor-image-upload.php
Last active December 27, 2015 10:09
A script for uploading image files within WordPress using Redactor.js This script will ensure that images uploaded via Redactor are added to the media library in WordPress.
<?php
// Make sure required $_FILES values are set before trying to load WordPress
if ( isset( $_FILES['file'] ) ) {
// Load WordPress
require( strstr( dirname( __FILE__ ), 'wp-content', true ) . 'wp-load.php' );
// Load additional required files since we did a short init
@wpscholar
wpscholar / redactor-image-upload.php
Last active December 27, 2015 10:09
This script handles WordPress image uploads when uploading files using Redactor.js. It uses the 'SHORTINIT' parameter to prevent loading plugins and unnecessary files. NOTE: Images uploaded using this script are NOT imported into the WordPress media library.
<?php
// Make sure required $_FILES values are set before trying to load WordPress
if ( isset( $_FILES['file'] ) ) {
// We don't need all of WordPress, just the basics
define( 'SHORTINIT', true );
// Load WordPress
require( strstr( dirname( __FILE__ ), 'wp-content', true ) . 'wp-load.php' );
@wpscholar
wpscholar / get-value.php
Last active October 14, 2021 21:05
Get a value from an object or an array. Allows the ability to fetch a nested value from a heterogeneous multidimensional collection using dot notation.
<?php
/**
* Get a value from an object or an array. Allows the ability to fetch a nested value from a
* heterogeneous multidimensional collection using dot notation.
*
* @param array|object $data
* @param string $key
* @param mixed $default
* @return mixed
@wpscholar
wpscholar / excerpt.php
Created January 10, 2014 21:31
Generate an excerpt from provided content. Strips HTML, removes trailing punctuation and adds a 'more' string when text has been removed.
<?php
/**
* Get an excerpt
*
* @param string $content The content to be transformed
* @param int $length The number of words
* @param string $more The text to be displayed at the end, if shortened
* @return string
*/
@wpscholar
wpscholar / array-order-by-keys.php
Created February 11, 2014 17:26
Order an array based an ordered set of keys. NOTE: Values that don't have a corresponding key in the array of keys will be dropped.
<?php
/**
* Order an array based an ordered set of keys.
* NOTE: Values that don't have a corresponding key in the array of keys will be dropped.
*
* @param array $array
* @param array $keys
* @return array
*/
public static function array_order_by_keys( array $array, array $keys ) {
@wpscholar
wpscholar / array-find.php
Created February 11, 2014 17:28
Case insensitive array_search() with partial matches
<?php
/**
* Case insensitive array_search() with partial matches.
*
* @param string $needle
* @param array $haystack
* @return bool|int|string
*/
public static function array_find( $needle, array $haystack ) {
@wpscholar
wpscholar / index-by.php
Last active September 20, 2017 23:19
Index a collection of arrays/objects by a specific key/property.
<?php
/**
* Index a collection of arrays/objects by a specific key/property.
*
* @param string $index
* @param array $data
* @return array
*/
function index_by( $index, array $data ) {
@wpscholar
wpscholar / validate.php
Created February 11, 2014 17:32
Validate a value by running it through one or more callbacks.
<?php
/**
* Validate a value by running it through one or more callbacks.
*
* @param mixed $value
* @param callable $validation
* @return bool
*/
function validate( $value, $validation ) {
@wpscholar
wpscholar / sanitize.php
Created February 11, 2014 17:33
Sanitize a value by passing it through one or more sanitization callbacks.
<?php
/**
* Sanitize a value by passing it through one or more sanitization callbacks.
*
* @param mixed $value
* @param callable|array $sanitize
* @return mixed
*/
function sanitize( $value, $sanitize ) {
@wpscholar
wpscholar / wp-error-merge.php
Last active July 21, 2021 16:37
Merge multiple WP_Error objects together
<?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();