Skip to content

Instantly share code, notes, and snippets.

Avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / get-term-ids.php
Created February 21, 2023 14:32
WordPress: Get term IDs, creates terms if they don't exist
View get-term-ids.php
<?php
/**
* Gets the term IDs for the term names. Creates the terms if they do not exist.
*
* @param string $term_names The term name (or names separated with pipes).
* @param string $taxonomy The taxonomy.
* @return array
*/
function get_term_ids( $term_names, $taxonomy ) {
@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
View slow-db-queries.php
<?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
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 July 8, 2022 14:04
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 June 8, 2022 19:35
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 April 4, 2022 17:09
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 January 18, 2022 21:59
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 November 18, 2021 15:51
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 November 3, 2021 19:45
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 17:12
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 :)