Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / wp-multi-level-parse-args.php
Created March 28, 2024 19:43
WordPress Multi Level Parse Args
<?php
/**
* wp_parse_args() with support for multi-level arrays.
*
* @param array $a Arrays to be parsed
* @param array $b Defaults for the arrays.
* @return array
*/
function multi_level_wp_parse_args( &$a, $b ) {
@petenelson
petenelson / _tribe_blocks_recurrence_rules.json
Last active March 4, 2024 21:04
Sample _tribe_blocks_recurrence_rules for The Event Calendar
[
{
"type": "weekly",
"all_day": false,
"multi_day": false,
"start_date": "2024-03-11",
"_start_date_input": "March 11, 2024",
"_start_date_obj": "2024-03-11T05:00:00.000Z",
"start_time": "08:00:00",
"_start_time_input": "8:00 am",
@petenelson
petenelson / activate-memcached.sh
Created February 21, 2024 22:38 — forked from strategio/activate-memcached.sh
Setup memcached in a Local by Flywheel site
#!/usr/bin/env bash
PHP_VERSION=$1
CONF_FOLDER="/usr/local/etc/php/${PHP_VERSION}/"
PHP_INI="${CONF_FOLDER}php.ini"
cd ${CONF_FOLDER}
git clone --depth 1 https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached
/usr/local/bin/phpize
@petenelson
petenelson / composer.json
Created September 22, 2023 18:43
PHP Compatibility
{
"require-dev": {
"phpcompatibility/php-compatibility": "dev-develop"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
@petenelson
petenelson / get-term-ids.php
Last active March 28, 2024 19:45
WordPress: Get term IDs, creates terms if they don't exist
/**
* Gets the term IDs for the term names. Creates the terms if they do not exist.
* TODO update the project_name_term_id_created filter when you copy/paste this function.
*
* @param string $term_names The term name (or names separated with pipes).
* @param string $taxonomy The taxonomy.
* @param string $by Get term by 'name' or 'slug'?
* @param bool $create Create the term if it doesn't exist?
* @return array
*/
@petenelson
petenelson / slow-db-queries.php
Last active December 21, 2022 15:00
WordPress: Sample Code to debug slow DB queries when in WP-CLI
<?php
global $wpdb;
$wpdb->queries = [];
// Run some slow code here.
if ( ! empty( $wpdb->queries ) ) {
@petenelson
petenelson / filter-strip-all-tags.php
Created November 2, 2022 13:56
WordPress: FILTER_SANITIZE_STRING for PHP8
<?php
/**
* Callback filter for filter_var_array() to strip HTML tags.
*
* @return array
*/
function filter_strip_all_tags() {
return [
'filter' => FILTER_CALLBACK,
@petenelson
petenelson / cli-add-new-term.txt
Last active July 8, 2022 14:04
WordPress: CLI command to add a new term to posts based on an existing term
wp post list --taxonomy_name=existing-term-slug --format=ids | xargs -0 -d ' ' -I % wp post term add % taxonomy_name new-term-slug
@petenelson
petenelson / add-term-to-post.php
Created June 8, 2022 19:35
WordPress: Add term to post
<?php
$term = 'Some Term Name'
$taxonomy = 'some-taxonomy';
if ( function_exists( '\wpcom_vip_term_exists' ) ) {
$term_data = \wpcom_vip_term_exists( $term, $taxonomy );
} else {
$term_data = term_exists( $term, $taxonomy ); // phpcs:ignore
}
@petenelson
petenelson / hide-meta-boxes.php
Last active April 4, 2022 17:09
WordPress: Hide specific post editing meta boxes
<?php
$this->_add_action( 'current_screen', 'maybe_hide_meta_boxes' );
/**
* Hides various meta boxes if they have not already been hidden.
* Users can still unhide the meta box and it will not be hidden in
* the future.
*
* @param WP_Screen $screen The current screen object.