Skip to content

Instantly share code, notes, and snippets.

View chriskoelle's full-sized avatar

Chris Koelle chriskoelle

View GitHub Profile
@chriskoelle
chriskoelle / get-nested-value.js
Created October 11, 2021 18:52
Get a nested value from an object
/**
* Get a nested object value by path.
*
* @example
* var obj = {foo: {bar: 'baz'}};
* getNestedValue(obj, 'foo.bar'); // returns 'baz'
*
* @param {Object} obj The nested object.
* @param {String} path The object path.
* @return {any} The object value at the specified path.
@chriskoelle
chriskoelle / use-size.js
Last active February 22, 2021 18:00
React useSize hook
import { useRef, useState, useEffect } from 'react';
/**
* Get the size of a component on render and resize
*
* @example
* const sizeRef = useRef(null);
* const { height, width } = useSize(sizeRef)
*
* <div ref={sizeRef}>
@chriskoelle
chriskoelle / wildcardToRegExp.js
Last active July 17, 2020 20:44
Convert a string with asterisk wildcards to a RegExp
/**
* Escape RegExp special characters
*
* @param {string} str String to escape
* @return {string} Escaped string
*/
const escapeRegExp = (str = '') => str.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
/**
@chriskoelle
chriskoelle / class-user-registration-column.php
Last active May 22, 2019 11:27
WordPress - Add Registration Date as a sortable column for users
<?php
/**
* Add Registration Date as a sortable column for users
*/
class NH_User_Registration_Column {
function __construct() {
add_filter( 'manage_users_columns', array($this, 'manage_users_columns') );
add_filter( 'manage_users_custom_column', array($this, 'manage_users_custom_column'), 10, 3 );
add_filter( 'manage_users_sortable_columns', array($this, 'manage_users_sortable_columns') );
@chriskoelle
chriskoelle / max-image-size.js
Created December 16, 2014 20:06
Use javascript and canvas to resize images from file input before upload
jQuery(function($) {
// Test for file API support.
var fileApiSuport = !!(window.File && window.FileReader );
/**
* Resize images that exceed the maximum size (in pixels)
* @param {object} input The file input.
* @param {Function} callback Callback function run after the image has been resized.
* @return {null}
*/
@chriskoelle
chriskoelle / class-nh-shortcode-generator.php
Last active August 29, 2015 14:11
Add a button above TinyMCE to generate a shortcode
<?php
/**
* Create a button that generates a shortcode to be added to TinyMCE.
*
* @todo Add an option to change the template file path.
* Add the ability to specify what admin pages the button is displayed on.
*/
class NH_Shortcode_Generator {
private $form_id = null;
@chriskoelle
chriskoelle / mailchimp-archive.php
Last active August 29, 2015 14:07
Display a list of past MailChimp campaigns
<?php
class NH_MailChimp_Archive {
private $_args, $max_page,
public function __construct($args = array()) {
global $paged;
$defaults = array(
'apikey' => false,
'start' => 0,
'limit' => 12,
@chriskoelle
chriskoelle / class-nh-post-type-pages.php
Last active August 29, 2015 14:05
Allows you to use a page as a custom post type archive (similar to the "posts page" within reading settings)
<?php
class NH_Post_Type_Pages {
private $pfpt, $post_types;
function __construct() {
$this->get_post_type_pages();
add_action( 'admin_init', array(&$this, 'reading_settings_section') );
if(!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)):
add_filter( 'pre_option_show_on_front', array(&$this, 'filter_show_on_front'));
@chriskoelle
chriskoelle / gallery-extract.php
Last active August 29, 2015 14:04
Extract First Gallery from a post
@chriskoelle
chriskoelle / custom_page_templates.php
Last active August 29, 2015 14:02
Creates a submenu page for tools that lists out all custom page templates and which pages are using them. #wordpress
<?php
// Debug Custom Page Templates
function custom_page_templates_page() {
$templates = wp_get_theme()->get_page_templates();
$done = array();
$row = '<tr><td><a href="%s">%s</a></td><td width="60"><em>%s</em></td><td width="20"><a href="%s">edit</a></td></tr>';
$not_found = '<tr><td colspan="3"><em>No pages found using this template.</em></td></tr>';
echo '<div class="wrap"><div id="icon-tools" class="icon32"></div>';
echo '<h2>Custom Page Templates</h2>';
foreach ( $templates as $template_name => $template_filename ):