Skip to content

Instantly share code, notes, and snippets.

@csalzano
csalzano / add-php-version-to-admin-footer.php
Last active February 19, 2018 04:30
A WordPress plugin that adds the PHP version next to the WordPress version in the admin dashboard.
@csalzano
csalzano / remove-deleted-user-from-gform-entries.php
Created April 25, 2018 16:19
Gravity Forms does not dissociate deleted users from form entries. This code removes user IDs from entries when users are deleted.
add_action( 'delete_user', 'remove_deleted_user_from_gform_entries' );
function remove_deleted_user_from_gform_entries( $user_id ) {
//$user_id was just deleted, remove this from any entries if stored as created_by
global $wpdb;
$wpdb->query( $wpdb->prepare( "
UPDATE `{$wpdb->prefix}gf_entry`
SET `created_by` = NULL
WHERE `created_by` = %d
", $user_id ) );
@csalzano
csalzano / insert-term.js
Last active February 27, 2019 14:58
JavaScript that inserts a term inside a WordPress custom taxonomy
//insert a term into our Transmission custom taxonomy
let term = new wp.api.models.Transmission( {
name: 'Automatic',
description: 'A vehicle transmission that self-shifts between gear ratios',
slug: 'automatic'
} );
term.save();
@csalzano
csalzano / delete-term.js
Last active February 27, 2019 15:01
JavaScript that deletes a term from a WordPress custom taxonomy
//find & delete a term in our Transmission custom taxonomy
let terms = new wp.api.collections.Transmission();
terms.fetch().done( function( t ){
let target = t.find( x => 'automatic' === x.slug );
if( ! target ) { return; }
jQuery.ajax({
method: 'DELETE',
url: myplugin.rest_base_url + 'wp/v2/transmission/' + target.id + '?force=true',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', myplugin.nonce ); //must be wp_create_nonce( 'wp_rest' ), see paragraph 4 https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
@csalzano
csalzano / edit-term-meta.js
Created February 27, 2019 15:08
JavaScript that edits a term meta value in a WordPress custom taxonomy
//edit a term meta value in our Transmission custom taxonomy
let terms = new wp.api.collections.Tranmission();
terms.fetch().done( function( t ){
let target = t.find( x => 'automatic' === x.slug );
if( ! target ) { return; }
jQuery.ajax({
method: 'POST',
url: myplugin.rest_base_url + 'wp/v2/transmission/' + target.id,
//save the number 6 in a term meta key 'speeds'
data: { meta: { 'speeds': 6 } },
<div class="widget">
<div class="widget-content">
<h2 class="widget-title">At a Glance</h2>
<ul class="at-a-glance">
<li>Founded <b>1729</b></li>
<li>Size <b>23 square miles</b></li>
<li>Population <b>&lt;5,000</b></li>
<li><b>7</b> properties in the National Register of Historic Places</li>
</ul>
</div>
@csalzano
csalzano / a-provide-licenses.php
Last active March 11, 2019 15:54
A plugin that provides license keys to the EDD SL Updates on Multisite plugin for Inventory Presser add-ons
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: Make Licenses Available for Updates on Multisite
* Plugin URI: https://gist.github.com/csalzano/621deacc33f2482da205f294b445485a
* Description: A plugin that provides license keys to the EDD SL Updates on Multisite plugin for Inventory Presser add-ons
* Version: 1.0.0
* Author: Corey Salzano
* Author URI: https://coreysalzano.com/
* Text Domain: invp-make-licenses-available
$taxonomy_name = 'venue';
//Only include venues from the current year in REST API responses
add_filter( "rest_" . $taxonomy_name . "_collection_params", 'hide_old_venues', 10, 2 );
function hide_old_venues( $prepared_args, $request ) {
//Populate the default value of the include argument to only include terms with specific IDs in the meta box
$prepared_args['include']['default'] = get_terms( array(
'taxonomy' => $taxonomy_name,
@csalzano
csalzano / thickbox-allow-percent-size.js
Last active September 18, 2019 01:19
Allow the sizes of a thickbox.js modal to be specified as percentages of the viewport instead of pixels
Object.defineProperty(Number.prototype, 'percentOfViewportToPixels', {
enumerable: false,
value: function( heightOrWidth ) {
var viewport_size = 'height' == heightOrWidth
? Math.max( document.documentElement.clientHeight, window.innerHeight || 0 )
: Math.max( document.documentElement.clientWidth, window.innerWidth || 0 );
return Math.round( viewport_size * ( this / 100 ) );
}
});
@csalzano
csalzano / wporg-developer-get-template-part.php
Last active March 16, 2021 18:40
Change which template is served in a Twenty Twenty-One child theme to incorporate wporg-developer
<?php
//In single.php of twentytwentyone theme, find this line
get_template_part( 'template-parts/content/content-single' );
//and replace it with this condition to blend the code reference features of the wporg-developer theme
if( DevHub\is_parsed_post_type() )
{