WordPress Pass PHP Variable and Values to JS Theme or Plugin

The beautiful function wp_localize_script() was built for l11n, however its found as a method for carrying over server-side values into client-side JS.

Your values are now available in your .js´╗┐ file:

console.dir(my_theme_data);

Currently using this in a React theme I’m building for WordPress.

This can also be done with something like:

Every Image URL Filter For WordPress, Front & Back End

I had a project where every single media library image URL needed to be filtered, backend and front end. As far as I’ve found these were the filters for every area:

The myplugin_filter_html_image_urls() function peals out images with regex from areas where it’s not just the URL being sent. This function may require verification of the current domain incase bad practices of using external images is being done.

WordPress Pass Variable Into wp_footer, wp_head, & all Other Actions/ Hooks/Filters

Passing variables into WordPress hooks using an anonymous function I thought was impossible. I always worked around this and rewrote the logic. Today I found out anonymous functions support a use keyword, allowing passing:

So simple and easy. Shaves hours of troubleshooting.

Switching PHP Versions Localhost, Keep phpMyAdmin Working

When switching localhost php versions for testing, nothing interrupts the workflow more than phpMyAdmin failing because you’re on php 5.4.

The single line of code, to be your index.php in /phpmyadmin/ simply routes you to either the phpmyadmin/new/ (v4.7 – supports php 5.5-7.2) and phpmyadmin/old/ (4.0 – supporting php 5.2 – 5.4):

Simple.

On Img Upload, Create New Post, Set as Featured Img

I used to manually do everything for the Images section of my site. Adding an image would involve: save photo after editing, renaming it, uploading, creating new post, setting the title and date, assigning the featured image as the upload, then setting the category to Images so it’s added to the page. So, 8 steps, usually done over-and-over again to a dozen or so images in a batch. We’re talking over 96 steps to get a batch of images online – it took a bit of time, and was slightly demotivating, becoming a chore instead of a hobby.

Able to scratch my own itch – I came up with a way to automate everything. Instead of 96 steps, I now publish an entire batch of images in just 2 simple steps. Yes Two. Too. To. You read correctly, just 2! How’d I do it?

After editing the photos in Lightroom, I use Lightroom’s file-rename feature during bulk export that automatically sets the filename to the images meta timestamp, so YYYYMMDD.jpg.

I then use the following script tapping into the add_attachment hook during bulk upload that does the other 7 steps for each image. Everything in just 1 step, for the entire bulk.

https://gist.github.com/davidsword.ca4a6ec6cde878a8a47fde35feecab69

So, all I have to do after exporting from Lightroom, is drag-and-drop upload to the site.