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

Fifty Actions of WordPress – 50 Examples (31 to 40)

Read Time:9 minsLanguages:
This post is part of a series called Fifty Actions of WordPress.
Fifty Actions of WordPress – 50 Examples (21 to 30)
Fifty Actions of WordPress – 50 Examples (41 to 50)

If you're just joining us, we're currently working through 50 (of the many) actions that are available in WordPress. In the last post, we covered 21 - 30 so we'll keep the pace going with another set in this post.

If you haven't already, I recommend going back and reading the introduction and then reading through the following post so that you're brought up to speed with everything that we've covered thus far.

With that said, let's begin!

Controlling the Default JavaScript Code

WordPress has many scripts in its core that we can load. The wp_default_scripts action lets us interact with the default JavaScript files.

Removing "jQuery Migrate" From a WordPress Website

If you use jQuery code and you're absolutely sure that your code is compatible with jQuery version 1.9 or later, then you don't need the jQuery Migrate plugin which supports deprecated jQuery code. Here's how you can deregister it:

Just to be sure, test every jQuery-related output in your frontend and make sure they still work. You don't want to break your website.

The <head> of Your Front-End

This action is fired off in the wp_head() function so you could insert stuff into the <head> element of the front-end pages of your website.

Utilizing Open Graph in Your Website

Open Graph is a very important protocol to help big guys like Facebook, Google, and Twitter understand your pages. If you provide information with the Open Graph protocol, you can define featured images, titles, summaries and such, and help them build structured and valid data for your pages.

Let's see how we can utilize this neat protocol and inject Open Graph-related metadata in our pages' <head>s:

Initializing the Theme

Each time a theme's files has been loaded for a WordPress page, the after_setup_theme action is fired off. Since it's called on every page request, you can hook theme-related functions to this action.

Setting Up Theme-Specific WordPress Features

There are ways to enable WordPress features for themes like post formats or featured images, but the correct way would be to create a function for them and hook the function to the after_setup_theme action.

See, now our theme can have featured images, we can post videos or galleries, and our "Visual Editor" can have its own style file to make it feel more like the front-end.

Adding Custom Columns to the Media List

The manage_media_custom_column action helps us add custom columns to the list of uploaded files in the Media Library.

Displaying the ID of Each Upload in a Column

I hate the process of looking up the ID of a featured image that needs to be excluded from a gallery in the same post. I really do. Luckily, I found this code snippet to show me the IDs of uploaded files in the Media Library:

Hope you like it, too!

Comment Status Transitions

Every time a comment status changes ('approved', 'unapproved', 'spam' or 'trash'), an action hook named transition_comment_status will be fired. After that, another action will be called with variables in its name: comment_(old_status)_to_(new_status). Let's see how the second action works.

E-Mailing the Commenter After Their Comment Is Approved

Suppose you prevent comments to be published without you approving them first; but you also need to inform your visitors about their comments being published when they do. You can email them the good news by using these lines of code as a plugin:

Now the commenters will be informed when their comments are up. From there, they may visit your post to see their comments and maybe read others. A fantastic (and easy) way to get returning visitors!

Handling the Loading of Page Templates

WordPress uses what we call "page templates" to display different types of pages like a single post, the homepage, a 404 error, search results, archives and such. And the template_redirect action fires off when WordPress decides which template will be used.

Redirecting to the Post Permalink if There's Only One Result

Want to save your visitors a click? The code below helps you in a unique way: If there's only one post in the search results, the user will see the page of the post instead of the search result. 

A neat trick, if you ask me:

Now, if someone searches for "guacamole" and there's just one post that mentions guacamole, the visitor will automatically be redirected to that post. 

Cool, huh?

Handling WordPress Feeds

Feeds are one of the oldest features in WordPress and they still work like a clock. With the do_feed action (and other relevant actions), you can control how feeds are handled.

Removing WordPress Feeds Altogether

Removing feed links make feeds inaccessible for almost everyone, but if a visitor knows that you're using WordPress and they have knowledge how feeds work in WordPress, they might just try to add /feed/ after your website URL and reach the feeds. What if you actually need to disable the feeds? This code snippet will help you disable the feeds altogether:

Easy, right?

Manipulating the Toolbar

The Toolbar (formerly Admin Bar) was introduced in version 3.1 of WordPress and has become the target for both hatred and love – some wants to remove it completely, some can't live without it. Anyway, the admin_bar_menu action is the main hook for the Toolbar which loads necessary items into the bar.

Always Show the Toolbar to Everyone

The toolbar might come in handy if it doesn't look too bad with your website design – you can use the built-in search box and add new menu items like a link to log in or a link to the contact page.

The code snippet below demonstrates a toolbar which is always shown whether the visitor is logged in or not:

See how easy it is? Now your visitors can see "Log In" and "Contact" links along with the default search box.

Controlling the Default "Categories" Widget

You can display a list of categories with the wp_list_categories() function. And this action (which has the same name with the function) helps us customize the output.

Removing the title Attributes of Category Links

If you don't think that you need the title attributes of the category links in the widget, you can remove them by using these lines of code in your project:

Handling the Search Form Before Processing

In order to use the built-in search feature, you need to use the get_search_form() function. If you want to monkey with the function before the output is displayed, the pre_get_search_form() is your guy.

Prepending Informational Text Before the Search Form

Let's say you have a blog where you review hundreds, maybe thousands of commercial products. To inform your visitors about they can enter their barcode numbers to get to the product they need, use the following code to add some informational text before the search box.

Note that you can use this example with the example of the template_redirect action so when visitors enters a barcode number, they automatically go to the product review.

End of Part Four

We went through the fourth batch of 50 actions in this article. I hope you liked and learned new things from it. Play around with what you've seen and then I'll see you in the next article.

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.