Skip to content

Instantly share code, notes, and snippets.

View psorensen's full-sized avatar

Peter Sorensen psorensen

  • 10up
  • United States
View GitHub Profile
@psorensen
psorensen / config-wp-cache.md
Last active October 14, 2019 16:03
Setting up cache on WP Local Docker
@psorensen
psorensen / paginated-query.php
Created September 17, 2018 03:12
WP Paginated Query
<?php
use WP_CLI;
/**
* Paginated Query that performs callback for each iteration.
*
* @param string $callback Name of the callback function.
* @param array $args WP_Query arguments.
* @param string $message Message passed to WP CLI make_progress_bar.
<?php
/**
* Functionality for modifying and outputting breadcrumbs provided by Yoast.
*
* @package IPM
*/
namespace IPM\Breadcrumbs;
/**
@psorensen
psorensen / delete_uncategorized.bash
Created December 6, 2017 21:05
Bash script to remove 'uncategorized' term from posts with existing category terms
#!/bin/bash
for post in $(wp post list --field=ID --url={url} )
do
count=$(wp post term list $post 'category' --fields='name' --format="count" --url={url} )
if [ "$count" -gt "1" ]
then
wp post term remove $post category 'uncategorized' --url={url}
fi
done

Generate Search & Replace Script For All Applicable http-to-https Image Assets.

  1. run wp --allow-root db query "select post_content from wp_posts where post_content like '%<img src%';" --skip-column-names --silent | grep -oP "<img\s+.*?src=['\"](.*?)['\"].*?>" | grep -oP "https?://[^\"]*" | sort | uniq
  2. using parse_url(), create a PHP function* to find unique hosts and add first occurence of each to an array of links.
  3. Itterate through new list of links and run through a CURL function** to determine
  4. Add links with 200-300 status to an array
  5. Produce Search & Replace script based of returned values

Function Reference

@psorensen
psorensen / term-created-event.js
Created July 24, 2017 01:53
Wordpress: Fire event when term is created.
$( document ).ajaxComplete( function( event, xhr, settings ) {
try {
var queryStringArray = settings.data.split('&');
} catch(e) {
var queryStringArray = {};
}
// only when
if ( $.inArray( 'action=add-tag', queryStringArray ) !== -1 ) {
const $xml = $( xhr.responseXML );
@psorensen
psorensen / tinymce-add-formats.md
Last active August 23, 2019 06:24
Wordpress/TinyMCE - Add elements to formats dropdown

Adding Elements to the TinyMCE Format Dropdown

On a recent migration project, one of the requirements was to add a few blockquote styles to the TinyMCE dropdown list to match the editorial process of the old CMS. Wordpress provides a filter to add a secondary or tetriary dropdown, but if you only have a couple additional elements, it seems like bad UX to seperate it from the original dropdown.

Going off a tip from Chancey Mathews, I realized that Wordpress does not send a argument for block_formats when initializing TinyMCE, thus relying on the defaults. By adding this argument to the tiny_mce_before_init filter, we can add in our extra elements*:

* Note: since we're overriding the defaults, we need to include the original elements (p, h1-h6..etc)

function mce_formats( $init ) {
Worker information
hostname: i-0388d96-precise-production-2-worker-org-docker.travisci.net:d28a5fd8-3589-4295-975d-59cd61e40876
version: v2.5.0 https://github.com/travis-ci/worker/tree/da3a43228dffc0fcca5a46569ca786b22991979f
instance: e0656f8:travis:php
startup: 518.8849ms
system_info
Build system information
Build language: php
Build group: stable
Build dist: precise
@psorensen
psorensen / edd_create_subscription.php
Last active January 24, 2017 19:02
create EDD subscription for user email
<?php
private function om_add_nfg_subscription( $args ) {
// check if user already exists
$user = get_user_by('email', $args['email']);
$user_id = $user ? $user->data->ID : null;
// register new user if doesn't exist
if (! $user_id )
@psorensen
psorensen / edd-recurring-create.php
Created January 20, 2017 19:28
EDD Recurring Creation
$args = array(
'expiration' => date( 'Y-m-d 23:59:59', strtotime( 'January 3, 2018', current_time( 'timestamp' ) ) ),
'created' => date( 'Y-m-d 23:59:59', strtotime( current_time( 'timestamp' ) ) ),
'status' => 'pending',
'profile_id' => 0,
'transaction_id' => 0,
'initial_amount' => 1,
'recurring_amount' => 700,
'bill_times' => '1',
'period' => 'yearly',