Skip to content

Instantly share code, notes, and snippets.

View jtsternberg's full-sized avatar
😎
Awesome Motive-ing

Justin Sternberg jtsternberg

😎
Awesome Motive-ing
View GitHub Profile
@ColinHarrington
ColinHarrington / .gitconfig
Last active August 29, 2015 13:56
.gitconfig
[branch]
autosetuprebase = always
[mergetool "p4merge"]
cmd = /home/<username>/dir/to/p4v/bin/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
keepTemporaries = false
trustExitCode = false
keepBackup = false
[merge]
@lswilson
lswilson / category-to-cpt.sql
Last active August 29, 2015 13:58
WordPress: SQL to migrate posts from a single category to its own custom post type
UPDATE wp_posts
inner join wp_term_relationships rel on wp_posts.id=rel.object_id
inner join wp_term_taxonomy tax on rel.term_taxonomy_id=tax.term_taxonomy_id
inner join wp_terms term on tax.term_id=term.term_id
set wp_posts.post_type='your_post_type'
WHERE
term.slug="category_name"
@jtsternberg
jtsternberg / $-cache-with-find.js
Last active January 22, 2018 08:44
jQuery selector cache with reset (original: http://eamann.com/tech/selector-caching-jquery/). If commenting, please ping me on Twitter, same username.
function Selector_Cache() {
var elementCache = {};
var get_from_cache = function( selector, $ctxt, reset ) {
if ( 'boolean' === typeof $ctxt ) {
reset = $ctxt;
$ctxt = false;
}
var cacheKey = $ctxt ? $ctxt.selector + ' ' + selector : selector;
@jtsternberg
jtsternberg / cmb-select-with-optgroups.php
Last active November 15, 2017 20:38
CMB2 select field options overridden with a filter and using optgroups
<?php
function cmb_opt_groups( $args, $defaults, $field_object, $field_types_object ) {
// Only do this for the field we want (vs all select fields)
if ( '_cmb_option_field' != $field_types_object->_id() ) {
return $args;
}
$option_array = array(
'Group 1' => array(
@jtsternberg
jtsternberg / cmb2-number-field.php
Last active February 28, 2022 03:30
CMB2 Number Field
<?php
$cmb->add_field( array(
'name' => __( 'Postive numbers', 'theme-domain' ),
'desc' => __( 'Numbers only', 'msft-newscenter' ),
'id' => $prefix . 'number',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
@jtsternberg
jtsternberg / wds-include-tlc-transients.php
Last active August 29, 2015 14:07
tlc-transients helper functions
<?php
// Include tlc lib
require_once WPMU_PLUGIN_DIR . '/wp-tlc-transients/tlc-transients.php';
/**
* Use in place of `get_posts`
*
* @param array $args Array of get_posts arguments
* @param integer $time Amount of time before cache expires
@jtsternberg
jtsternberg / cmb-toggle-fields.css
Last active May 11, 2017 20:30
hidden-toggle for cmb. Works like the WordPress metabox toggle (but for only a selection of fields)
.advanced-toggle .toggle-label {
cursor: pointer;
display: block;
line-height: 3em;
border-bottom: 1px solid #e9e9e9;
border-left: 1px solid #e9e9e9;
border-right: 1px solid #e9e9e9;
padding-left: .8em;
}
.advanced-toggle .inside .cmb-row {
@jtsternberg
jtsternberg / post-endpoints.php
Last active September 25, 2017 05:02
Add custom post endpoints and misc. functionality
<?php
/**
* Handly place to define our endpoints all in one spot
* @return array Array of our endpoints
*/
function prefix_custom_endpoints() {
return array(
'portfolio',
'testimonials',
INITIALISATION
==============
load wp-config.php
set up default constants
load wp-content/advanced-cache.php if it exists
load wp-content/db.php if it exists
connect to mysql, select db
load object cache (object-cache.php if it exists, or wp-include/cache.php if not)
load wp-content/sunrise.php if it exists (multisite only)
@jtsternberg
jtsternberg / ajax-endpoint.js
Last active February 1, 2021 06:00
Proof of concept for avoiding admin-ajax for ajax callback requests. Also see Thomas Griffin's excellent post: https://thomasgriffin.io/a-creative-approach-to-efficient-and-scalable-wordpress-api-endpoints/ AND Josh Pollock's excellent post: http://torquemag.io/improved-wordpress-front-end-ajax-processing/
jQuery(document).ready(function($){
$('body').on( 'click', '.some-button', function(){
$.ajax( ajax_endpoint_data.api_url, {
type : 'POST',
dataType : 'json',
data : {
action: 'ajax_action',
some_data: 'some_value'
}