Skip to content

Instantly share code, notes, and snippets.

Avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / filter-strip-all-tags.php
Created Nov 2, 2022
WordPress: FILTER_SANITIZE_STRING for PHP8
View filter-strip-all-tags.php
<?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 Jul 8, 2022
WordPress: CLI command to add a new term to posts based on an existing term
View cli-add-new-term.txt
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 Jun 8, 2022
WordPress: Add term to post
View add-term-to-post.php
<?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 Apr 4, 2022
WordPress: Hide specific post editing meta boxes
View hide-meta-boxes.php
<?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 Jan 18, 2022
WordPress: Get All Descendants
View get-all-descendants.php
<?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 Nov 18, 2021
WordPress: Term Report CLI
View term-report-cli.php
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Generate a term list/report.
*
* ## OPTIONS
*
* <taxonomy>
@petenelson
petenelson / remove-anonymous-object-filter.php
Created Nov 3, 2021
WordPress: Remove an anonymous object filter
View remove-anonymous-object-filter.php
<?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
WordPress: wp_parse_args() with multidimensional array support.
View multi-level-wp-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 ) {
// 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 Sep 27, 2021
WordPress: Timestamp to date/time
View timestamp-to-date-time.php
<?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 ) {
View namespaced-function.php
<?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";
}