Skip to content

Instantly share code, notes, and snippets.

@scottnelle
scottnelle / cli.php
Last active May 10, 2024 15:26
A template WP CLI command and subcommand for iterating through batches of posts and doing something. Includes a progress bar.
<?php
/**
* WP CLI Command: commandname
*
* @package commandname
*/
WP_CLI::add_command( 'commandname', 'Commandname_CLI_Command' );
/**
Scott Nellé has been a web software developer for over 15 years. Prior to Alley Interactive, Scott built developer tools and WordPress web applications for Union Street Media. He developed Simple 301 Redirects, a popular WordPress plugin for SEO practitioners—currently active on over 300,000 sites.
He graduated from Champlain College with a B.S. in web site development and management. In his spare time he enjoys playing music, autumns in Vermont and spending time with his cats.
@scottnelle
scottnelle / woocommerce-exclude-categories.php
Last active September 13, 2019 11:32
Exclude specific categories from being considered when WooCommerce selects related products. $exclude is an array of category names or slugs. Note that WooCommerce caches products selected by category for a day, so once you install this code you'll have to save a product to clear the cache and see the results.
<?php
/**
* Exclude specific categories from being considered when selecting related products
*/
function my_exclude_related_terms( $terms ) {
$exclude = array( 'First Category', 'second-category' );
foreach ( $terms as $i => $term ) {
if ( in_array( $term->name, $exclude ) || in_array( $term->slug, $exclude ) ) {
unset( $terms[ $i ] );
}
@scottnelle
scottnelle / legacy-url-redirect.php
Created January 12, 2016 22:20
Redirect legacy urls to WordPress posts. Assumes that the legacy URL was stored to post meta as legacy_url during migration.
<?php
function redirect_legacy_urls() {
if ( ! is_404() ) {
return;
}
$url = trim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' );
if ( empty( $url ) ) {
return false;
}