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 / 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 ) {

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 / 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',
@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.
add_action('pmxi_attachment_uploaded', 'my_attachment_uploaded', 10, 3);
function my_attachment_uploaded($pid, $attid, $filepath){
$attachment = get_post($attid);
// do something with $attachment file
if (get_post($pid) == 'industry-resource') {
wp_update_post(array('ID' => $attid, 'guid' => str_replace('uploads/', 'uploads/indres/', $filepath)));
}
}
@psorensen
psorensen / Yoast_SEO_JS_fix
Created February 13, 2015 14:36
Yoast SEO JS Fix
function yst_clean(a){if(""==a||void 0==a)return"";try{a=jQuery("<div/>").html(a).text(),a=a.replace(/<\/?[^>]+>/gi,""),a=a.replace(/\[(.+?)\](.+?\[\/\\1\])?/g,"")}catch(b){}return a}function ptest(a,b){a=yst_clean(a),a=a.toLowerCase();var c=a.match(b);return null!=c?'<span class="good">Yes ('+c.length+")</span>":'<span class="wrong">No</span>'}function removeLowerCaseDiacritics(a){var b,c=[{base:"a",letters:/[\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250]/g},{base:"aa",letters:/[\uA733]/g},{base:"ae",letters:/[\u00E6\u01FD\u01E3]/g},{base:"ao",letters:/[\uA735]/g},{base:"au",letters:/[\uA737]/g},{base:"av",letters:/[\uA739\uA73B]/g},{base:"ay",letters:/[\uA73D]/g},{base:"b",letters:/[\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253]/g},{base:"c",letters:/[\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184]/g},{ba
<?php
/**
* Functionality for modifying and outputting breadcrumbs provided by Yoast.
*
* @package IPM
*/
namespace IPM\Breadcrumbs;
/**
@psorensen
psorensen / common.js
Last active February 15, 2018 14:06
Sage 9 Alpha main.js
export default {
init() {
jQuery(document).ready(() => $('.slick').slick());
},
finalize() {
// JavaScript to be fired on all pages, after page specific JS is fired
},
};
@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