Fifty Actions of WordPress – 50 Examples (21 to 30)

In this series, we're taking a look at WordPress actions: a type of hook that the application offers that allows us to customize execution. In the last article, we looked at the second set of 10 actions bringing us up to 20 that we've covered thus far.

In keeping with the spirit of the previous articles, we're going to take a look at another 10 actions along with examples of each. 

With that said, let's resume.

Manipulating get_posts() Before It's Processed

The pre_get_posts action handles one of the most important query functions: get_posts().

Including Custom Post Types to the Search Results

Let's say you run a movie review blog and you need the "Movies" post type to show up in the search results. You can include any post type you want with the help of the following lines of code:

Voilà! Now your website's search results will include the "Movies" post type in addition to regular posts.

Working With Post Status Changes

There are actually many actions for post status transitions – draft_to_publish, new_to_future, publish_to_private and so on. WordPress gathers this set of actions and calls it {$old_status}_to_{$new_status} in the Codex.

But if you need an action to keep an eye on all status changes, you can use the transition_post_status action.

Emailing the Administrator Each Time a Post Status Has Been Changed

Imagine you're running a multi-author blog with three editors, and you need information about each post status change. If that's the case, you can use the code snippet below:

Enqueuing Scripts for Admin Pages

If you ever need to inject a JavaScript file into the administration panel of your website, the admin_enqueue_scripts action is for you: This handy little action is responsible for the enqueuing of the scripts (and the styles) inside the WordPress dashboard.

Injecting a Script for the Post Add/Edit Screens

Let's say you created a special meta box but you need a JavaScript file in your plugin's folder to make the meta box work. What do you do? You don't print a <script> tag in your code – you use the code below!

Controlling the Process of Saving Posts

As its name suggests, this useful action is called when a post is saved to the database.

Require a Featured Image Before Post Publishing or Scheduling

Sometimes, I forget to set a featured image for my posts, although all my posts should have one. While searching for an answer for my problem, I stumbled accross this elegant solution:

A long but awesome example, am I right?

Adding Meta Boxes for Post Types

Meta boxes are arguably one of the fundamental reasons WordPress is the most flexible content management system in the world. It's true: You can crate almost anything as a meta box and let your users create post data with them. And the add_meta_boxes action is the main hook we use to create meta boxes.

 Creating a Simple Meta Box

We're going through actions and I don't want to digress from the topic, so I'm just going to show you how the action is used: By filling a function with the add_meta_box() function(s) and hooking it to the add_meta_boxes action:

[tip]If you want to learn more about creating custom meta boxes, check out this amazing tutorial on this topic written by Christopher Davis.[/tip]

Toying with the "At a Glance" Section

The "At a Glance" section, formerly named "Right Now", is the primary widget of the dashboard of every WordPress installation. With the activity_box_end action, you can safely play with that area.

Putting Your Contact Information Under the "At a Glance" Section

Let's say you're a freelance WordPress developer and you want your clients to remember your phone number. With the code snippet below, you can leave a note for them and put your phone number under the "At a Glance" section:

Don't put your personal number, though: You wouldn't want to be called up at 11 p.m. on Saturday.

Manipulating the Default "Meta" Widget

There are many default widgets that WordPress offer in its core, and the "Meta" widget is one of them. With the help of the wp_meta action, we can customize the widget any way you like.

Adding a "Follow Us on Twitter" Link to the "Meta" Widget

Granted, the "Meta" widget isn't the most popular one among WordPress' default widgets, but you can make it more functional and appealing by adding extra lines and links to it, like so:

Of course, this is just a cheesy example but hey, it works!

Playing With the Dashboard

The "dashboard" is the main page of the administration panel of every WordPress installation. With the wp_dashboard_setup action, you can customize the dashboard any way you like.

Displaying Latest Disqus Comments in a Dashboard Widget

If you use Disqus in your comments section (without the official plugin) and want to see latest comments in your dashboard, you can use the code snippet below to add a dashboard widget:

Change the variables $disqus_username $number_of_comments and you're good to go!

Setting the Current User

Oh look, an action for a pluggable function! WordPress defines "pluggable functions" like this:

These functions can be replaced via plugins. If plugins do not redefine these functions, then these will be used instead.

And this handy little action is a part of the pluggable wp_set_current_user core function, which changes the current user by ID or name.

Removing the Toolbar to Subscribers

We're not going to change the user now, but instead we're going to take advantage of the action and check the capabilities of the current user, then disable the toolbar if the user is just a subscriber:

Loading Plugins

If you need things done after WordPress finishes loading activated plugins, you can count on the plugins_loaded action.

Start Your Plugin

The correct way to initialize your plugin and make it run is hooking its main function to the plugins_loaded action. Here, we have the simplest example in the world:

If you need a better example, and I'm sure you do, you should definitely check out Tom McFarlin's "WordPress Plugin Boilerplate" which has everything you need to build a WordPress plugin with the concept of object-oriented programming in mind.

End of Part Three

We went through the third batch of 50 actions in this article. I hope you liked and learned new things from it. See you in the next one!

I want to hear your thoughts, too. What do you think about these actions? Post your comments below; and if you liked the article, don't forget to share it!

Tags:

Comments

Related Articles