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:
<?php add_filter( 'registration_redirect', 'redir_new_users' ); function redir_new_users() { // change the slug below with your own page's slug return home_url( '/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:
<?php add_filter( 'login_redirect', 'send_subscribers_home', 10, 3 ); function send_subscribers_home( $location, $request, $user ) { global $user; if ( isset( $user->roles ) && is_array( $user->roles ) ) { if ( in_array( 'subscriber', $user->roles ) ) { return home_url(); } else { return $redirect_to; } } return; } ?>
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:
<?php add_filter( 'logout_redirect', 'goodbye_subscribers', 10, 3 ); function goodbye_subscribers( $location, $request, $user ) { global $user; if ( isset( $user->roles ) && is_array( $user->roles ) ) { if ( in_array( 'subscriber', $user->roles ) ) { // change the slug below with your own page's slug return home_url( '/goodbye/' ); } else { return $redirect_to; } } return; } ?>
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:
<?php add_filter( 'lostpassword_redirect', 'lostpassword_message' ); function lostpassword_message( $location ) { // change the slug below with your own page's slug return '/lost-password-notice/'; } ?>
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:
<?php add_filter( 'comment_post_redirect', 'redirect_to_newsletter' ); function redirect_to_newsletter( $location ) { // change the slug below with your own page's slug return '/subscribe-to-our-newsletter/'; } ?>
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):
<?php // use the built-in __return_true() WordPress function to simply return a "true" boolean statement add_filter( 'press_this_redirect_in_parent', '__return_true' ); // return the home URL to redirect the author to the homepage add_filter( 'press_this_save_redirect', function( $url, $id, $status ) { return home_url(); }, 10, 3 ); ?>
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!
Comments