This tutorial is short, sweet, simple and to the point, and I believe it has some good use for those who have membership sites. Today we are going to learn how to redirect users after they update/edit their profiles in WordPress. The code is fairly simple and only requires a little bit of work.
As of now, whenever a user updates their profile, they are simply shown a message saying that their settings have been saved. While that is nice, it leaves your user wondering what is next or where to go. This can be especially frustrating to those new users who have never seen the WordPress admin interface before. We can alleviate this situation by automatically redirecting them to a specified page after they have updated their profiles.
Pretty compact function, right? We are hooking into the
profile_update action hook, which is fired once a user clicks the save button. We are hooking into this action and telling WordPress to redirect our user to the home page after they have updated their profile. The
12 at the end of the function is for our function priority. We want this function to fire at the very end of the hook to make sure that all of the data has been saved. This should also help to avoid any potential conflict with plugins that may add extra fields of meta data to profile pages as well. By setting the low priority, all the updated options will be saved before we initiate our redirect.
We use the
wp_redirect function to do all the redirecting for us. It accepts two parameters:
$status. Since we aren’t sending any error statuses with this, nor are we specifying any specific type of redirect, we will just leave that parameter empty. As mentioned in the Codex,
wp_redirect needs to be followed by the PHP command
exit; immediately after it has been called. This terminates the function and makes sure that the last thing happening is the actual redirect to our specified url.
Adding Some Extra Conditional Options
This is a pretty sweet function, no lie, but as it currently stands, this will redirect -any- type of user after they update their profile, including administrators. You probably don’t want this to be happening, so we can add a little bit of conditional logic to specify when we want this function to occur. Check out the function below:
We’ve added some conditional logic to this using the
current_user_can function. This function checks roles and capabilities for WordPress users. With this function, we can specify which types of users we want this function to work for. In the example above, this function will only work if your user has the role of ‘subscriber’. You can set this to an array of roles, or even apply it to all user types except for administrators. Mold it however you would like 🙂
I hope you have enjoyed this tutorial! If you’ve found it helpful, be sure to let me know and share it with your friends!