Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@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().
*
@petenelson
petenelson / composer.json
Created September 22, 2023 18:43
PHP Compatibility
{
"require-dev": {
"phpcompatibility/php-compatibility": "dev-develop"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
@petenelson
petenelson / get-term-ids.php
Last active March 28, 2024 19:45
WordPress: Get term IDs, creates terms if they don't exist
/**
* Gets the term IDs for the term names. Creates the terms if they do not exist.
* TODO update the project_name_term_id_created filter when you copy/paste this function.
*
* @param string $term_names The term name (or names separated with pipes).
* @param string $taxonomy The taxonomy.
* @param string $by Get term by 'name' or 'slug'?
* @param bool $create Create the term if it doesn't exist?
* @return array
*/
@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
<?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
<?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
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
<?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 25, 2025 13:59
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>