Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / remove-anonymous-object-filter.php
Created November 3, 2021 19:45
WordPress: Remove an anonymous object filter
<?php
/**
* Remove an anonymous object filter.
*
* @param string $tag Hook name.
* @param string $class Class name
* @param string $method Method name
* @return void
*/
function remove_anonymous_object_filter( $tag, $class, $method ) {
@petenelson
petenelson / multi-level-wp-parse-args.php
Last active May 5, 2022 17:12
WordPress: wp_parse_args() with multidimensional array support.
/**
* 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 ) {
// https://mekshq.com/recursive-wp-parse-args-wordpress-function/
// Similar to wp_parse_args() just a bit extended to work with multidimensional arrays :)
@petenelson
petenelson / timestamp-to-date-time.php
Created September 27, 2021 14:39
WordPress: Timestamp to date/time
<?php
/**
* Formats a timestamp into a date and time based on WordPress settings.
*
* @param int $timestamp The timestamp.
* @param string $format Optional date time format.
* @return string
*/
function timestamp_to_date_time( $timestamp, $format = false ) {
@petenelson
petenelson / namespaced-function.php
Created March 8, 2021 21:39
Namespaced PHP function.
<?php
/**
* Quickly provide a namespaced way to get functions.
*
* @param string $function Name of function in namespace.
* @return string
*/
function n( $function ) {
return __NAMESPACE__ . "\\$function";
}
@petenelson
petenelson / get-paginated-query-results.php
Created September 28, 2020 19:48
WordPress: Get paginated query results.
<?php
/**
* Runs WP_Query and gets all results using pagination.
*
* @param array $query_args List of WP_Query args.
* @return array
*/
function get_paginated_query_results( $query_args ) {
@petenelson
petenelson / output-css-classes.php
Last active February 7, 2024 15:30
WordPress: Output CSS classes
/**
* Outputs a list of sanitized CSS class names.
*
* @param array|string $classes List of class names (array or string with
* class names separated by spaces or commas).
* @param bool $echo Echo the list of class names (defaults to true).
* @return void|array
*/
function output_css_classes( $classes, $echo = true ) {
@petenelson
petenelson / cache-time-and-query.php
Last active June 14, 2022 19:43
WordPress: Cache time and query
<?php
/**
* Returns a random cache time, defaults to between 11 and 12 hours.
*
* @param integer $min Min time in seconds.
* @param integer $max Max time in seconds.
* @return integer
*/
function get_cache_time( $min = HOUR_IN_SECONDS * 11, $max = HOUR_IN_SECONDS * 12 ) {
@petenelson
petenelson / raw-rewite-rules.txt
Created December 10, 2019 16:07
WordPress Default rewrite_rules option value
a:89:{s:11:"^wp-json/?$";s:22:"index.php?rest_route=/";s:14:"^wp-json/(.*)?";s:33:"index.php?rest_route=/$matches[1]";s:21:"^index.php/wp-json/?$";s:22:"index.php?rest_route=/";s:24:"^index.php/wp-json/(.*)?";s:33:"index.php?rest_route=/$matches[1]";s:52:"blog/category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$";s:52:"index.php?category_name=$matches[1]&feed=$matches[2]";s:47:"blog/category/(.+?)/(feed|rdf|rss|rss2|atom)/?$";s:52:"index.php?category_name=$matches[1]&feed=$matches[2]";s:28:"blog/category/(.+?)/embed/?$";s:46:"index.php?category_name=$matches[1]&embed=true";s:40:"blog/category/(.+?)/page/?([0-9]{1,})/?$";s:53:"index.php?category_name=$matches[1]&paged=$matches[2]";s:22:"blog/category/(.+?)/?$";s:35:"index.php?category_name=$matches[1]";s:49:"blog/tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$";s:42:"index.php?tag=$matches[1]&feed=$matches[2]";s:44:"blog/tag/([^/]+)/(feed|rdf|rss|rss2|atom)/?$";s:42:"index.php?tag=$matches[1]&feed=$matches[2]";s:25:"blog/tag/([^/]+)/embed/?$";s:36:"index.php?tag=$matc
@petenelson
petenelson / gist:f314edc59a422fdf8e940fdd45c03050
Created October 11, 2019 14:07
WordPress Cache Incrementor
/**
* Gets or updates the cache incrementor for a post type.
*
* @param string $post_type The post type.
* @param bool $update Whether to force an incrementor update.
* @return string
*/
function post_type_cache_incrementor( $post_type, $update = false ) {
$group = 'post_type_cache_incrementor';
@petenelson
petenelson / reset-term-order-bug.php
Last active March 26, 2019 18:58
wp_update_post() resetting term order
<?php
// Sample code to demonstrate a possible bug in core where
// wp_update_post() is resetting the term_order.
global $wp_taxonomies;
// Let core set the term_order field automatically.
$wp_taxonomies['category']->sort = true;
$post_id = wp_insert_post(