Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. WordPress

Fifty Actions of WordPress – 50 Examples (11 to 20)

This post is part of a series called Fifty Actions of WordPress.
Fifty Actions of WordPress – 50 Examples (1 to 10)
Fifty Actions of WordPress – 50 Examples (21 to 30)

In the previous part of this series, we started going through 50 WordPress actions, selected among hundreds and we started by reviewing 10 of them. In this tutorial, we're going to see another batch and do examples with each.

Let's begin!

Handling Default WordPress Styles

WordPress has many CSS files for its back-end and front-end to use. With the wp_default_styles action, we can monkey with the default styles of WordPress.

Removing ie.css From the WordPress Admin Panel

If you're the only one using the administration panel of your WordPress website and you're not using Internet Explorer, there's no need to load the IE-fixer CSS file, right? 

You can use the code below to get rid of the ie.css:

Actually, I'm not even sure that WordPress needs this file anymore – after all, ie.css fixes things that looks bad on IE7 and below and as far as I know, the usage percentage of IE7 has dropped below 1%. 

We should suggest a patch, don't you think?

Handling the get_footer() Function

If you need to work with the get_footer() function, you don't need to look any further – you can use the action with the same name, get_footer!

Injecting In-page JavaScript Into the Footer

Let's say you have some jQuery goodness you need to print at the footer of your web pages. You can use these bits of code to make it work:

There. We used some JavaScript code for this example but you can practically run any code to your footer.

Initializing the Admin Panel

This handy little function fires every time an admin page is visited, so it has many different uses. Get creative!

Keeping Non-Admin Users Away From the Admin Panel

Let's say you don't want your subscribers to be able to visit the administration panel and you don't have any contributors, authors or editors. To redirect all the non-admin users to homepage, you can use the code snippet below:

If you want, you can change the redirection adddress from your homepage to something else: Just delete the site_url() bit and enter the desired address with single quotes (like 'http://www.google.com/').

Stepping in to the Process of Authentication

The inline documentation defines this action simply with these sentence: "Runs to authenticate a user when they log in."

Allowing Email Addresses as Usernames While Logging In

WordPress doesn't allow users to log in with their email addresses – you have to remember your username. If your user base tends to forget their usernames, you can use the code snippet below and tell your users that they can log in with their email addresses, too:

Now, your users can enter their email addresses instead of their usernames.

Playing With the Login Form

The login_form action lets us manipulate the output of the classic WordPress login form.

Displaying a Warning in the Login Form

In the previous example, we showed you how to let your users use their email addresses instead of usernames. If you're not a fan of this behavior, however, you can warn your users about the fact that they can't use their email addresses:

Of course, you can put other warnings like "Don't click on the 'Remember Me' checkbox if you're on a shared computer!" or a funny one like "If you're under threat by a robber while you're logging in, enter your password backwards and WordPress will automatically call the police – but wait, if you're reading this, then the bad guy will read it too... RUN!". You can also use HTML.

Handling Admin Menu Items

Defined as "runs after the basic admin panel menu structure is in place", the admin_menu action lets us add or remove menu items (and sub-menu items) into/from the WordPress admin panel's menu.

Removing Menu Items That Shouldn't Be Seen by Clients

It's a familiar scenario for freelance WordPress developers: A client needs access to a certain plugin's "Options" page, but they shouldn't be touching any settings in WordPress' own "Options" pages. 

In short, there are pages that clients need to access and there are ones that they shouldn't access. The example below helps us remove menu items from the main admin menu:

Comment out or delete the lines you don't want, and you're good to go!

Working With the wp() Function

Let's see what the documentation says about this action:

"Executes after the query has been parsed and post(s) loaded, but before any template execution, inside the main WordPress function wp(). Useful if you need to have access to post data but can't use templates for output."

In short, it fires off after the query's been loaded. Simple, like its name.

A Quick Way to Schedule Cron Jobs in WordPress

While cron jobs are usually hooked to a plugin activation hook, we can also use the wp action to hook our cron jobs to. Let's see the example provided by the Codex:

Notice that there's another action named prefix_hourly_event – that action is created automatically in the same code snippet, right inside the wp_schedule_event() function, as its third parameter.

Controlling the <head> in the Admin Panel Pages

There are various hooks (actions and filters) that have "variables" in their names. The admin_head-(page_name) action is one of them, which is called in the <head> for a specific admin page which is defined in the variable.

Changing the Columns Count in the Dashboard

I use a 22'' monitor and since WordPress version 3.8, I'm forced to use 4-column Dashboard which is kind of annoying for me. I'm not sure why I can't set a number of columns like I could earlier, but I found a quick solution to the issue:

Now I can change the number of columns like we used to – as long as my screen width allows. I still can't choose over 2 columns on my laptop, but I think I can live with that.

Modify the Toolbar Before It's Rendered

The WordPress Toolbar, formerly Admin Bar, is a great and useful navigation element that helps us in both front-end and back-end. And the wp_before_admin_bar_render action helps us interact with it before it's rendered.

Adding a New Item to the Toolbar

If you want to provide a quick link for your clients to reach you, you can use these lines of code to add a link to their site's Toolbar:

Easy, right? You can use the add_node() function again to create as many links you like.

Processing Profile Updates

The profile_update hook lets us fetch and work with the user data right after it's updated in the database.

Informing the User About the Profile Update

Let's say that you want to inform users each time they update their profiles. With the help of our handy action and a small function, you can do it:

In my opinion, this is a simple yet effective security measure. That said, it wouldn't be effective at all if a potential hacker changes the user's email address, since the email will be sent to the new email address.

End of Part Two

We went through the second 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. If you liked the article, don't forget to share it!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.