Tag Archives: WordPress

PSA: Don’t (necessarily) Trigger a Save from a Save in WordPress

I know what you’re thinking. John got caught in an endless loop, and now he’s sharing his stupidity with us.

Good guess. Normally you’d be right. This is not the case this time, though.

Say you need to process some data and it takes a while. Normally you’d render the data in a shortcode, but you don’t want to run all that churn on the front end while your readers are waiting. Makes sense to process the data and inject it on save so it’s just in the content, right?

Yeah, actually! That’s a great idea!

Maybe something like this?

NOOOOOOO. Don’t do that. I see you, thinking you’re clever, removing the function from its hook and re-adding it. Well your function might not be the only one hooked into a save. Now what do you think about that, huh? Not so smart, now, are we?

What you want is the wp_insert_post_data filter:

wp_insert_post_data runs before the database is updated and lets you modify post data before the update or insert happens. It has an optional 2nd argument containing the old post data as well, so you can check what’s been updated too, if necessary.

Note To Self: Keep Transient Names Short

In case anybody has this problem: I was trying to set a transient for a certain plugin and using an API key in the transient. This seemed like a great idea, since the transient would automatically be invalidated if the user ever updated their API key. Super simple.

But the transient wouldn’t set. No matter what, I’d hit my breakpoints inside the ‘if not set’ code block.

Well, as it turns out, if you set an expiration time, WordPress will also set a secondary transient option: _transient_timeout_{$transient_key}. That’s a lot of extra characters padding the left side of that option name! 19, in fact.

So with only 64 characters allowed in the option_name column of wp_options to begin with, you need to limit your transient names to 45 characters or fewer. In my case, the api key I was working with was 36 characters, which left me just 9 characters with which to uniquely prefix my transient.

I had 17.

Oops.

TinyMCE Advanced as a Network Activated Plugin

If you’ve ever needed the TinyMCE Advanced plugin┬áby Andrew Ozz, you know that the configuration process can be somewhat tedious.

If you’ve ever had to install it as a network activated plugin on a multisite installation (especially one with more than just a few sites), you probably thought about ditching the plugin altogether.

At work, I’m in the process of building a multisite installation that will consist of more than a dozen sites and they need this plugin. Being the lazy coder I am, I came up with this quick script to force TinyMCE Advanced to use site-wide options rather than options on a per-site basis.

Continue reading