Skip to content

Instantly share code, notes, and snippets.

@joemaller
joemaller / wordpress-is_term.md
Created Mar 12, 2020
A simple is_term() conditional function for WordPress
View wordpress-is_term.md

Even though individual Tags, Categories and Taxonomies are all represented as WP_Term objects, WordPress has three separate conditional tags for sorting them out: is_tag(), is_category() and is_tax(). These are sometimes combined into a single conditional statement in WordPress core, and there are plenty of examples on GitHub where developers have created an umbrella is_term() function like this:

function is_term($term = '') {
    return is_tag($term) || is_category($term) || is_tax($term);
}

It would be nice to have that function added to WordPress core.

@joemaller
joemaller / crontab
Created Mar 11, 2020
Add this to a user's crontab on macOS to auto-remove downloads more than 14 days old
View crontab
# Removes downloads older than 14 days. Runs every 6 hours
45 */6 * * * find ~/Downloads -maxdepth 1 -mtime +14 -exec rm -rf {} \;
@joemaller
joemaller / README.md
Last active Sep 23, 2021
Fix term counts so wp-admin Taxonomy listings show the correct number of items for the selected post_type.
View README.md

Fixing post counts for WordPress taxonomies

The problem

For WordPress sites using Custom Post Types, Taxonomy admin listings show cumulative term-assignment totals for all post_types instead of counts specific to the selected post_type. Since Taxonomy listings are always attached to a single post_type, totals for shared taxonomies don't make sense.

The Goal

Fix term counts so wp-admin Taxonomy listings show the correct number of items for the selected post_type.

View my_bulk_action_admin_notice.php
<?php
add_action('admin_notices', function () {
if (!empty($_REQUEST['bulk_emailed_posts'])) {
$count = (int) $_REQUEST['bulk_emailed_posts'];
$msg = _n(
"Emailed {$count} post to Eric.",
"Emailed {$count} posts to Eric.",
$count,
'email_to_eric'
@joemaller
joemaller / wordpress-template-loader.php
Created Feb 18, 2020
The template-sorting array from WordPress' wp-includes/template-loader.php
View wordpress-template-loader.php
<?php
$tag_templates = array(
'is_embed' => 'get_embed_template',
'is_404' => 'get_404_template',
'is_search' => 'get_search_template',
'is_front_page' => 'get_front_page_template',
'is_home' => 'get_home_template',
'is_privacy_policy' => 'get_privacy_policy_template',
'is_post_type_archive' => 'get_post_type_archive_template',
'is_tax' => 'get_taxonomy_template',
View kenken-2020-01-13.json
{
"width": 6,
"height": 6,
"values": [1, 2, 3, 4, 5, 6],
"rules": [
{
"op": "-",
"value": 3,
"cells": [
{ "x": 0, "y": 0 },
@joemaller
joemaller / compressing-all-images-wordpress.md
Last active Jun 25, 2020
How to make sure all images uploaded to WordPress are optimized
View compressing-all-images-wordpress.md

Compressing All Uploaded Images with WordPress

A Not Unreasonable Assumption

A lot of WordPress users assume, rightly, that images they've uploaded are compressed and optimized for the web.

This assumption is mostly true, but sometimes poorly-compressed or oversized images will slip through. We can't expect every website user to know the intricasies of image compression, and it's not unreasonable to expect that WordPress should make all uploaded images work correctly.

So let's do that. Here's how to make sure every image uploaded to WordPress is optimized.

@joemaller
joemaller / error_log-kint-debug.php
Last active Dec 20, 2019
snippet for dumping Kint-php to error_log as ANSI text, then resetting Kint back to normal
View error_log-kint-debug.php
<?php
// DEBUG
\Kint::$mode_default = \Kint::MODE_CLI;
error_log(@d($val));
\Kint::$mode_default = \Kint::MODE_RICH;
View normalizeAcfLinkField.php
/**
* Normalize output from the ACF Link field
*
* Always returns an array, if the field is empty, it returns this empty array:
* ['title' => '', 'url' => '', 'target' => '']
*
* Additionally, if the title is empty, it is populated from the url
*
* Target will be either an empty string or a fully-formed html attribute, like this:
* The array ['target => '_blank']
@joemaller
joemaller / ideasonpurpose-templates.code-snippets
Last active Nov 11, 2019
Wrap templates in HTML comments which include the filepath
View ideasonpurpose-templates.code-snippets
// In VS Code, choose "Preferences: Configure User Snippets" from the Command Palette,
// then choose "New Global Snippets file..." and add this. It's preset to trim file
// paths from "template-parts" or "_includes".
//
// To trigger the snippet, first select the text to be wrapped, choose "Insert Snippet"
// from the Command Palette, then select "Template Start/Stop Comments".
{
"Template Start/Stop Comments": {
"scope": "php,html",
"prefix": "Template Comments Wrap",