Skip to content

Instantly share code, notes, and snippets.

@joemaller
joemaller / child-filepath.md
Last active June 17, 2022 15:13
Get PHP child Class file paths from inherited parent class methods

Get PHP child Class file paths from inherited parent class methods

While refactoring some code into a reusable PHP Class I hit a brief roadblock where some code expected the [__FILE__ magic constant][file] file path. Since that value always refers to the specific file it's called from, it won't work from a separate Class file because the referenced path would be the parent instead of the child.

The full filepath of a child Class can be inferred from an inherited parent Class method by combining [get_class($this)][get_class] or [get_called_class()][get_called_class] with the [ReflectionClass::getFileName][getfilename] method like this:

// ParentClass.php
class ParentClass
{
@joemaller
joemaller / postmortem.md
Created April 20, 2020 20:24
recap of tracking down a very tricky bug

Postmortem on a multi-layered bug

Also, a very long, mostly miserable day.

The problem

A web project was recently updated but now won't build on some computers. The updates largely involved migrating the project to our [webpack-based Docker toolset][docker-build] which is in use on other projects and is supposed to be stable. Updates were authored on Macs, then tested on Windows -- where the whole project failed to run.

"THIS IS IMPOSSIBLE"

@joemaller
joemaller / README.md
Last active April 2, 2020 19:13
A snippet for wrapping a selection in HTML comments which include the file name

Wrapped template fragments

When developing websites, the dev team at Ideas On Purpose has found it very useful to wrap template fragments in named HTML comments. During development and debugging, these waypoints reveal the exact files and structure of how how the page was assembled.

Most files look something like this:

<?php
  // a bunch of logic which populates the $pages variable
?&gt;
@joemaller
joemaller / wordpress-is_term.md
Created March 12, 2020 14:33
A simple is_term() conditional function for WordPress

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 March 11, 2020 21:39
Add this to a user's crontab on macOS to auto-remove downloads more than 14 days old
# 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 February 16, 2024 15:00
Fix term counts so wp-admin Taxonomy listings show the correct number of items for the selected post_type.

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.

<?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 February 18, 2020 19:27
The template-sorting array from WordPress' wp-includes/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',
{
"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 June 23, 2023 17:50
How to make sure all images uploaded to WordPress are optimized

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.