I know you're not "supposed" to debug things by outputting them into or past the footer of a site, but sometimes you just need to check on something real quick, like the ID or some other bit of data about a post, and you don't feel like dealing with any proper debugging tools. The wp_footer hook then gets used and the results don't make sense. What happened?
Tag: php
Enqueue inline JS at the WordPress footer hook
WooCommerce has a handy function you might like to include in your toolbag even when that plugin isn't in play: wc_enqueue_js(). It takes a string of JS code, wraps it in jQuery, outputs it in the footer of your site, and runs it when the DOMContentLoaded event fires. Neat? I guess!
Keep on readin' on Enqueue inline JS at the WordPress footer hook⟶
You can't filter meta data in WordPress for some reason
By that I mean that the handy and heavily-used get_post_meta() function, and the get_metadata() function for which the former is a wrapper, do not call apply_filters() on the data they return. Of all the places I've found lacking support for hooks, this one is perhaps the most infuriating.
Keep on readin' on You can't filter meta data in WordPress for some reason⟶
Limiting dynamic WPML translations to one instead of hundreds
This isn't limited to WPML (or even WordPress), of course, as it's about translating dynamic strings in general. But SEO best practices say you must have a very specific keyword focus in your title and whatnot or your words will never be seen by human eyeballs ⸺ that your efforts will be for naught but to train some unknown but all-knowing AIs that will never pay you for your contribution.
Keep on readin' on Limiting dynamic WPML translations to one instead of hundreds⟶
Pulling your color palette from theme.json into ACF color pickers
I'm not sure why ACF doesn't do this by default as it seems like something of a no-brainer. If a given theme has a color palette set, why wouldn't the user want that palette available in each color picker? Seems like an obvious and easy win to me.
Keep on readin' on Pulling your color palette from theme.json into ACF color pickers⟶
Including Gutenberg patterns with WPML involved
I previously posted about how to render patterns in your templates using their ID or slug. With WPML involved, things may get a little more complicated.
Keep on readin' on Including Gutenberg patterns with WPML involved⟶
Include Gutenberg patterns in your theme templates
Patterns are pretty nice when building a site for a client since they can be reused and inserted easily. If they're not the synced sort, ones that are updated everywhere at once, they can even be inserted and then edited for the needs of a given page or post.
Keep on readin' on Include Gutenberg patterns in your theme templates⟶
Simpler WordPress Ajax calls
This has been possible for years, but it's hard to know everything, you know? That's especially true when the thing you want to use isn't even documented and it's up to your fellow nerds to figure it out.
Resize images with WordPress's image editor in PHP
Say you've got a list of external images to output that may be huge and mongous. In my case it was images of houses up for rent from a big-ass XML feed. It's unwise to serve these images up directly to users given that huge payloads are generally frowned upon. What you need, then, is a function that takes an image URL and caches it locally as a resized variant.
Keep on readin' on Resize images with WordPress's image editor in PHP⟶
Debug PHP variables discreetly
If you don't have Xdebug involved in your current project, you may be relying on var_dump or var_export to check on things while coding. Depending on where in the code this happens, their output can get in the way of the rendering of a given page or be hard to read given where it lands ⸺ in one part of a three-column block, for instance.