How to redirect users after successful login – Learn WordPress from a to z

Tutorials 0 lượt xem

When developing a membership website, one of the most common issues you need to think about is user management.

There are dozens of plugins that come with a bunch of different features for user management. However, if you only need a few simple features, then you should think about developing them yourself.

Today, I want to show you how to redirect users to different pages after they successfully login to your WordPress site.

User redirect after login

User redirect after successful login

I will use a filter hook called login_redirect . This hook is very useful in doing redirects after login, so I will use this hook as follows:

add_filter( 'login_redirect', 'hk_login_redirect', 10, 3 );<div><span>1</span></div>

After adding the filter hook, I will create the callback function referenced in the hook above named hk_login_redirect .

function hk_login_redirect() {

    global $user;

    if ( isset( $user-&gt;roles ) &amp;&amp; is_array( $user-&gt;roles ) ) {

        if ( in_array( 'administrator', $user-&gt;roles ) ) {

           // redirect defaukt

           return $redirect_to;

        } else {

           return home_url();

        }

    } else {

        return $redirect_to;

    }

}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span>
</div>

You must replace the $redirect_to variable with the URL that you want to redirect the user to after they successfully log in.

You can either put the above code in the functions.php file or create it as a standalone plugin to manage user redirects after login.

The complete code will look like below:

add_filter( 'login_redirect', 'hk_login_redirect', 10, 3 );

function hk_login_redirect() {

    global $user;

    if ( isset( $user-&gt;roles ) &amp;&amp; is_array( $user-&gt;roles ) ) {

        if ( in_array( 'administrator', $user-&gt;roles ) ) {

        // redirect defaukt

        return $redirect_to;

        } else {

           return home_url();

        }

    } else {

        return $redirect_to;

    }

}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span>
</div>

summary

I hope you can apply this article in your WordPress theme or plugin development. Especially when you need to create a website related to WordPress user management.

If you found this article useful, please comment and share this article. In addition, you can follow the WordPress Tips section and follow Facebook for more new knowledge.

Bài viết liên quan