Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / wp-multipart-image-upload.php
Last active February 26, 2025 16:44
WordPress: Upload image to REST API via multipart/form-data body
<?php
/**
* Creates a multipart/form-data body for an image and form fields.
*
* @param string $file_path The path to the file.
* @param string $filename The base filename, if different from the file path.
* @param array $fields The form fields.
* @return array The boundary ID and the body.
*/
@petenelson
petenelson / find-all-shortcodes.php
Created September 11, 2024 14:14
WordPress: Find All Shortcodes
<?php
$regex = get_shortcode_regex();
// Find all registered tag names in $content.
preg_match_all( "/$regex/", $content, $matches );
if ( ! empty( $matches ) ) {
var_dump( $matches[2] );
}
@petenelson
petenelson / insert-into-array.php
Last active July 23, 2024 18:53
PHP: Insert item into an array
<?php
/**
* Inserts an item into an array.
*
* @param array $array The array to insert into.
* @param mixed $key The array key to insert the item at, either before or after.
* @param mixed $new_key The new array key.
* @param mixed $new_item The new item to insert.
* @param bool $before Insert before or after?
@petenelson
petenelson / wp-cli-delete-posts-terms.php
Last active June 19, 2024 18:39
WP-CLI: Delete all terms or posts
wp term list taxonomy --field=term_id | xargs wp term delete taxonomy
wp post delete $(wp post list --post_type='page' --format=ids) --force
@petenelson
petenelson / any-empty.php
Created May 23, 2024 19:54
PHP: Determines if any of the supplied vars are empty().
<?php
/**
* Determines if any of the supplied vars are empty().
*
* @param arra ...$vars List of vars.
* @return bool
*/
function any_empty( ...$vars ) {
foreach ( $vars as $var ) {
@petenelson
petenelson / get-pagination-counts.php
Last active May 6, 2024 17:07
WordPress: get_pagination_counts()
<?php
/**
* Gets pagination counts for a query.
*
* @param WP_Query $query The query, defaults to global wp_query.
* @return array
*/
function get_pagination_counts( $query = false ) {
global $wp_query;
@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 / wordpress-sanitizers.php
Last active December 26, 2024 15:54
WordPress Sanitizers
<?php
/**
* Sanitize helper function to retrive values from $_GET, $_POST, etc.
*/
namespace ProjectName\Sanitizers;
/**
* Gets a sanitized text field from an array. Defaults to sanitize_text_field().
*