Fifty Actions of WordPress – 50 Examples (41 to 50)

If you've been following with us through this series, then you know we're in the homestretch in looking at our 50 actions of WordPress. For those who are just joining us, I urge you to check out the previous article (as this picks up right where it left off) as well as the articles linked from each one prior.

That will bring you up to speed with where we're headed now.

Let's begin!

Injecting to the <head> of Plugin Admin Pages

Plugins have needs, too: They may need in-page scripts or styles for their own option pages. With the admin_head-(plugin_page) action, it's possible to inject things into the <head> tag for specific plugin pages.

Adding Styling to Your Plugin Admin Page

If you ever need to add some CSS styling to your plugin's options page, the code below will help you with that:

Change the commented-out line with your CSS code, replace the second part of the action name (tools_page_myplugin/myplugin) with your own plugin and you're good to go!

Handling Pings Before They're Processed

"Pings" are one of the oldest features of WordPress and the pre_ping action lets us handle the pings before they're processed.

Disable Self-Pings

WordPress doesn't distinguish internal links from external links, when it comes to pings. To disable self-pings, you can utilize this handy little code snippet:

From now on, your WordPress installation will not ping its own posts.

Working With the get_header() Function

The get_header action is called when the template calls the get_header() function, making it perfect for functions that modify the header of WordPress' front-end.

Activate a Simple Maintenance Mode

If you're in a hurry and don't have time to install a "maintenance mode" plugin and set its options, you can simply use the code below and give a wp_die() error to everyone except administrators:

Since only administrators (and super admins) have the 'activate_plugins' capability, the website will be shut down to everyone except admins.

Tampering With the <head> of the Login Page

The login_head action helps us control the <head> tag on the login page.

Removing the Shake Effect for Incorrect Credentials

If you don't like that "shake" effect when a user submits incorrect login information, you can use the function below to remove it:

I like the effect, though.

Working With the Footer of the Dashboard

On occasion, we might want to control the footer of our admin panels – not the footer section per se, but the part before the  </body> tag. The admin_footer action does exactly that.

Adding Quick Styles for Post Statuses

Having a consistent set of colors is one of the reasons that makes WordPress' admin panel beautiful, but I don't think there's any harm in some color-coding for things that need to be separated visually – like different post statuses. 

If you're like me and want to be able to distinguish published posts from drafts or other post statuses, use the code below:

Enqueuing Scripts and Styles in the Login Page

We can enqueue stuff to the frontend with wp_enqueue_scripts, and we can enqueue stuff to the back end with admin_enqueue_scripts. What about the login page? You guessed it: This time the login_enqueue_scripts is our hook!

Changing the Logo Above the Login Form

I like the WordPress logo, but I don't think it should be shown every time my users log in to my websites. If you think the same way, you can replace the WordPress logo with your own by using this useful code snippet below:

Put the login-logo.png file into the /images/ folder of your theme and you're good to go!

Adding Custom Columns to the Users List

You know the user list in the "All Users" page in the admin panel? The manage_users_custom_column action allows us add new custom columns to that list with the help of an accompanying filter.

Displaying Registration Dates of Users in a Column

Suppose you need to see your members' registration dates in bulk. You can check your database records each time you need that information, or you can use this code snippet to add an extra column to the Users list:

Now you know more about your members.

Working With Plugin Activations

What do you do when you need to check when a plugin is activated in WordPress? Well, you use the activated_plugin hook: This handy little action is fired on plugin activation.

Sending an Email to the Admin Each Time a Plugin Is Activated

Suppose you have lots of client websites (which were installed with your email address) and you need to be informed when clients installs and activates a new plugin on their websites. 

Just use this function and hook it to activated_plugins and you're good to go:

Handling the Color Scheme Options

Since WordPress 3.0, we have "color schemes" for the admin panel and we're allowed to edit, add or remove the color schemes. And the admin_color_scheme_picker action makes it possible for users to change the color scheme.

Removing the Option to Change Color Schemes

This example doesn't need much introduction: If you ever need to take away the right to change color schemes from your users (say, because you have a special color scheme and you don't want your users to change it back to the default), use the code snippet below to remove the option:

Hey, we just removed a function from an action hook with the same name. I know, it's weird.

Handling the Process of Logging Out

Users log in, users log out, and when they log out, the wp_logout action is called.

Redirecting User to the Homepage After Logout

Logging out from a WordPress website is kind of weird: You're redirected to the login page, like WordPress needs you to log in again. Here's how you fix the situation and redirect users to homepage when they log out:

Now each time a user logs out, they will see the the homepage instead of the login form.

End of Part Five

We've just completed through through the last batch of 50 actions in this article. I hope you liked and learned new things from it. In the next article, we're going to have a quick look at what we've seen and close the series. 

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

Tags:

Comments

Related Articles