The publish_post hook

The publish_post hook is one I’ve seen a lot in tutorials and such. Even has its own page in the codex.

But surprise! That hook doesn’t actually exist, at least not if you search for do_action\(\s?.publish_post.

When I was learning to dig into core, this baffled me. What was this publish_post sorcery? Why wouldn’t it work on my custom post types???

Here’s a secret: The actual hook in core looks like this "{$new_status}_{$post->post_type}". It’s part of a small function in wp-includes/post.php (line 3321 at the time of writing), and there’s another equally useful variable hook right before it.

function wp_transition_post_status($new_status, $old_status, $post) {
	do_action('transition_post_status', $new_status, $old_status, $post);
	do_action("{$old_status}_to_{$new_status}", $post);
	do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}

There are quite a few of these variable hooks in core, I encourage you to go look at them, here’s a list I scraped together:

Continue reading The publish_post hook

Stronger than dirt… I mean Ajax

I first learned about Ajax at a WordCamp (yay!), but I didn’t fully grasp how to implement it. There was a helper script provided, and I figured out how to make it work for my plugins, but all I really knew was copy & paste.

It was at least a good 6 months before I dug in and looked at the WordPress Ajax API. Getting a closer look helped me understand what I was writing, and how I could simplify and/or make improvements when necessary.

So let’s start.

Continue reading Stronger than dirt… I mean Ajax

Screencast: Coding a plugin, start to finish*

This screencast is an experiment I wanted to try and if well-received, I may do more in this style.

This recording is in realtime, not sped up (I may do a timelapsed version in the future), so you can see what I’m doing. I try to narrate it to make it easier to follow, but sometimes the cat in the background throws me off.

Unedited. Exporting and uploading took long enough as it is, as my first screencast of this type, consider it a rough draft.

The final result of this screencast can be found here: https://github.com/trepmal/featured-image-sizes/tree/v1

However, the goal of this isn’t so much for the code at the end as the how I got there. Feedback and questions welcome.

Props to @dougal for the plugin idea. It might not be exactly what was had in mind, but that’s beside the point 🙂

* I know, plugins are never really finished

WordPress Talk Ideas

Supposing I were to take the time to write up a talk on some WordPress subject (probably development related), and then supposing I were to actually give this talk in front of a group of people, what do you think it should be about?

I’m considering being prepared with a talk proposal for an upcoming WordCamp – but I need help with brainstorming. Please, please, please offer a recommendation in the comments, or hit me up on Twitter.

I can’t promise that I’ll actually end up giving a presentation – whether out of failure to submit a proposal or out of failure to bribe someone to accept mine – but I will choose a topic and make every effort to prepare a talk/tutorial/presentation and share it here on trepmal.com.

My Favorite WordPress Resources

Really. There are a lot of WordPress resources out there, with fancy tutorials and snippets and thingamajigs, but 99% of my questions are answered in one of the above 2 sites.

Once I’ve got  piece of code working the way I want, I save it so I can refer to it later. I’m working on consolidating all my working code snippets, but for now they can be found on this site, or in github gists (as well as in some of my plugins). Some are still in the mu-plugins folder of my local development site, I’m working on getting those cleaned up and online.

Including Custom Fields Inside Your Post

WordPress/TinyMCE will sometimes remove code that you’d rather it not. For example, have you have tried to add a Google Map to a post without the aid of a plugin? You can’t. The iframes aren’t allowed.

Because of this, and a few other similar instances, I wrote a plugin that would let me easily bypass this filters. Continue reading Including Custom Fields Inside Your Post

Swap Color Schemes When in the Network Admin

In WordPress 3.1, the network-wide options being moved to their own area in WordPress to make it easier on network administrators.

To take a just a bit further, I put together this script that will switch color schemes when you’re in the Network Admin.

Continue reading Swap Color Schemes When in the Network Admin

Confirm Email Address

If you’re running a WordPress network, and depending on how you’re using it, you may need to get users to confirm or update their email address.

In my situation, I build a lot of sites for clients, and during development I use an email address of my own so they don’t get bombarded with emails they don’t need. But I don’t always change it back when I’m done, or maybe there’s a typo. Or maybe it’s the client that has changed their email address.

Whatever the cause, sometimes these things just need to be double-checked and confirmed. So here’s a small plugin that can be dropped in your mu-plugins folder:

Continue reading Confirm Email Address

Remove WordPress Plugin Update Count From the Menu

My office has a big WordPress multisite installation, so we are careful about updating plugins. As a result, there are ofter a number of plugins begging to be updated. It was getting annoying to see that all the time, so this little snippet gets rid of the notice. Continue reading Remove WordPress Plugin Update Count From the Menu