Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@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.
@petenelson
petenelson / get-all-descendants.php
Created January 18, 2022 21:59
WordPress: Get All Descendants
<?php
/**
* Gets all of the descendant/child post IDs for a parent post ID.
*
* @param int $parent_post_id The parent post ID.
* @return array
*/
function get_all_descendants( $parent_post_id ) {
@petenelson
petenelson / term-report-cli.php
Created November 18, 2021 15:51
WordPress: Term Report CLI
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Generate a term list/report.
*
* ## OPTIONS
*
* <taxonomy>
@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 ) {