Managing Redirections in WordPress With Filters

If you're new to the WordPress ecosystem, you might find it a little bit hard to grasp how flexible WordPress can be. Sure, there are literally tens of thousands of themes and plugins that you can make use of, and you may think WordPress is awfully extensible, but if you don't know about the "actions" and "filters" that WordPress provides within, you'll be surprised how much more you can do with this amazing content management system.

In this tutorial, I'm going to guide you through using WordPress filters to change the pages to redirect to upon different actions.

Let's get started!

Harnessing the Flexibility of WordPress With Actions and Filters

With the bold preaching I gave in the introduction, you might think that I exaggerated a bit. I assure you, I didn't. Hooks are, without a shadow of doubt, the actual reason for the flexibility of WordPress. Without the hooks system, themes would be way more rigid and, more importantly, most WordPress plugins wouldn't even exist. This would cause developers to develop fewer plugins and themes, and the world of WordPress would be much smaller, making it yet another dull content management system.

Hooks are meant to do what they are named after: to hook stuff. There are two types of hooks, action hooks and filter hooks. Action hooks fire functions before or after specific actions, and filter hooks handle the data before outputting it. (We'll be focusing on filter hooks in this tutorial.) That's why most plugins rely on hooks to control WordPress, and themes create their own hooks for other plugins to allow others to extend the themes.

I couldn't find a reliable source on when hooks were introduced in WordPress, but in the Version 1.2 announcement post on WordPress.org, a new plugin architecture is integrated and it says that "plugins can now hook into nearly every action WordPress does". If that's the very beginning of the introduction of WordPress hooks, it means that hooks were born in spring 2004, a year after the first version of WordPress was released.

If you want to learn more about actions and filters in WordPress, I have one series for each type of hook: "50 Filters of WordPress" and "50 Actions of WordPress". If you're interested, be sure to check them out!

Managing WordPress Redirection for Different Scenarios

As promised, I'm going to get to the WordPress filters now. We have seven WordPress filters to learn about in this tutorial: one for registrations, two for logins and logouts, one for password resets, one for comments, and two for the recently-revamped "Press This" feature.

Note: You can use the bits of code in this tutorial in your theme's functions.php file to get them into use quickly, but editing theme files for functionality purposes is considered a bad practice. (Check out my tutorial about "plugin territory".) Instead, you should make up a site-specific plugin for this purpose. (Check out Rachel's video tutorial about creating a plugin.)

Redirecting the User Upon Successful Registration

Normally, new users are redirected to the login form with this notice: "Registration complete. Please check your email." With the help of the registration_redirect filter, we're going to change that.

Let's say that you have a special white paper about your area of expertise, and you offer it to new users on your website. Using the code snippet below, you can send new users to a page with a download link to your white paper:

Edit the slug "white-paper" with your own page's slug and you're good to go!

Redirecting the User Upon Logging In and Out

After each login, users are sent to the dashboard. If your users don't need to (or shouldn't) see the dashboard for some reason, you can keep them in the front-end using the login_redirect filter.

In the following example, we're going to check the user role and redirect "subscribers" to the homepage:

The same can be done with the logout_redirect filter. Instead of sending them to the homepage, you can redirect them to a "goodbye" page where they can confirm that they've logged out:

Redirecting the User Upon Password Reset

By default, users who enter their usernames or email addresses in the "Reset Password" form are sent back to the login form with a notice to check their emails to reset their passwords. The lostpassword_redirect filter allows us to change that page.

The "Check your e-mail for the confirmation link" notice is sufficient for most WordPress websites, but if you need to share more information on the process of resetting passwords (like "Check your SPAM folder" or "Call this number if you haven't got any email"), you can redirect your users to a specific page:

Redirecting the User Upon Commenting

The comment_post_redirect filter lets you change the page your commenters go to after sending a comment.

This might be the best tip in this tutorial because it can increase your newsletter subscribers. For example, you can send them to a "Thanks for commenting, and don't forget to subscribe to our newsletter" page using the following lines of code:

Redirecting the User Upon Using "Press This"

"Press This" is a simple tool to quickly publish or save posts, but it has some limitations. For example, after you've used your website's "Press This" bookmarklet, it makes you return to the page you're sharing inside the popup window, while that page is already open in your browser. Weird, I know.

Thankfully, we can change that, with the help of the press_this_redirect_in_parent and press_this_save_redirect filters. Using the following code will make the "Press This" tool close the popup (first filter) and return to your own website's homepage (second filter):

Micro tip: Did you notice that I used an anonymous function? That's right: You can pass an anonymous PHP function if your server is running PHP v5.3.0 or later.

Wrapping Everything Up

The system of "hooks" is one of the most powerful features of WordPress, and it has helped the community make the gigantic mass of great plugins and themes we utilize today. Without "action hooks" and "filter hooks", it would probably be impossible to achieve this level of extensibility. And I think it's safe to say that without these hooks, WordPress wouldn't be used in one in every four websites. Whoever came up with the idea of these "hooks", thank you!

What do you think about the filters we've gone through in this tutorial? Can you think of better use cases for them? Or do you think I missed a hook that could be mentioned in this post? Share your thoughts, experiences and ideas with us in the Comments section below. And if you enjoyed reading this article, don't forget to share it with your friends and colleagues!

For anything else unrelated to this article, you can get in touch with me at @BarisUnver_EN (English) and @BarisUnver (Turkish) on Twitter.

Thanks for reading!

Tags:

Comments

Related Articles