If you are new to the WordPress ecosystem, you might find it a bit difficult to grasp its flexibility. Of course, there are literally tens of thousands of themes and plugins that you can use, and you may think WordPress is extremely extensible, and if you take notice of the "Actions" and "Filters" That WordPress provides, you will be surprised to see what you can do with it.

In this tutorial, I'll walk you through using WordPress filters to modify redirects. Remember that we have learned how to create a WordPress plugin. You will be able to use these codes in your plugin.

Let's start!

Harness the flexibility of WordPress with actions and filters

The Hooks Are, without a shadow of a doubt, the real reason for WordPress' flexibility. Without the system of " hooks ”, the themes would be much more rigid and, more importantly, most of the WordPress plugins wouldn't even exist.

The Hooks Or hooks are meant to do what their name refers to: clinging to specific things. There are two types of brackets, actions and filters.

Actions are triggered at a specific time, and the filters manipulate the returned data (nWe will work on the filters in this tutorial). That's why plugins rely on brackets to control WordPress, not to mention that some plugins and themes offer their own hooks (Hooks).

Redirection management for different scenarios

As promised, we will talk about filters. We have seven filters on WordPress that we will discuss during this tutorial.

Redirection when a user registers

Normally, new users are redirected to the online form connection after a regular registration.

However, you may want to redirect new members to a specific page on your blog. All you have to do is edit the filter that manages this reduction and add your code.


Redirection when connecting and or disconnecting

After each login, users access your dashboard. If your users do not have access, you can redirect them to another page. You can even make a condition that checks if the user is an administrator or not.

In the following example, we will check the role of the user and redirect the "Subscribers" to the home page:

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 filter " logout_redirect ". Instead of sending them to the home page, you can redirect them to a " goodbye Or the one of your choice.

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; }?>

Redirection after resetting a password

By default, users who enter their usernames or email addresses in the online form password reset messages are sent back to the online form connection. The filter " lostpassword_redirect Allows us to change this behavior.

If you need to share more information about the password reset process (checking spams also to find the verification email), you can redirect users to a specific page:


Redirection after a comment

The filter " comment_post_redirect Allows you to change the page to which users are redirected after posting a comment.

This might be the best tip in this tutorial as it can help you increase your subscriber list. For example, you can send them to a thank you page, and invite them to subscribe to your blog.


Redirection after the "Press This" action

« Press This Is a simple tool for publishing or saving articles quickly, but it has some limitations. For example, once you've used the bookmark " Press this From your website, it brings you back to the page you are sharing in the new open window, while the page is already open in your browser.

Fortunately, we can change this, with the help of the filters " press_this_redirect_in_parent " and " press_this_save_redirect ". Using the following code, the pop-up window will close after the action " Press This ».


Finally

That's about all you need to know for the moment. Do not hesitate to contact us if you want to share an opinion or in case of misunderstanding.