Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect code not executing

  • @ar_m

    Participant

    Hi everyone

    My site is using the latest WordPress 6.8.2 with WordPress 2025 theme and BBPress community

    In
    27. Custom Redirect After Login

    At address

    Layout and functionality – Examples you can use

    The following dual-purpose redirect code:

    /**
    * WordPress function for redirecting users on login based on user role
    */
    function my_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, ‘WP_User’ ) ) {
    if( $user->has_cap( ‘administrator’ ) ) {
    $url = admin_url();
    } else {
    $url = home_url();
    }
    }
    return $url;
    }
    add_filter(‘login_redirect’, ‘my_login_redirect’, 10, 3 );

    It is introduced that I have it in the php sippet file of the WordPress plugin code but I am getting the error

    Your changes are saved but your snippet was deactivated due to an error, please check the syntax and try again. Error message: syntax error, unexpected token ;

    It seems that there is a problem with the code.

    Can anyone help to fix the problem?

Viewing 4 replies - 1 through 4 (of 4 total)
  • @robin-w

    Moderator

    now corrected in codes, to this

    /**
    * WordPress function for redirecting users on login based on user role
    */
    function my_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if( $user->has_cap( 'administrator' ) ) {
    $url = admin_url();
    } else {
    $url = home_url();
    }
    }
    return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    @ar_m

    Participant

    Hello @robin-w

    The code is tested and works fine. Thank you for that.

    It seems that the code needs two modifications to be converted into a complete code. These two modifications are:

    1- Modification related to the user
    Absolutely directing the logged in user to the first page of the site is not useful in most cases, because

    Suppose the user has entered a forum topic and wants to reply to it. For this purpose, he enters the forum by entering his user information. In the default WordPress mode, the user who is on the current page is considered a logged in user and can send a reply

    However, with the above code, the user is redirected from the current page to the first page of the site and must return to the initial page of the topic in question with a few clicks. This causes user annoyance and increases the bounce rate from the forum.

    Therefore, the code needs to be modified so that the user on the current page (meaning any page of the forum or site that has a comment) is logged in on the same page as the current user after entering his user information and logging in, but at the same time does not have access to the counter.

    2- Correction related to the site and forum administrator
    The same problem as above is true for the administrator. For example, the administrator enters the forum and wants to create a topic or reply to a topic, so he enters the login information. In the default WordPress mode, he is considered logged in on the current page, but at the same time, the WordPress admin bar is displayed at the top of the forum or site, which means he has access to the counter.

    However, with the above code, the administrator is redirected from the current page to the counter, which should return to the original current page with a few clicks, just like the same user.

    Is it possible to modify the code as described?

    Thank you very much in advance for your valuable help.

    @robin-w

    Moderator

    sorry, beyond free help – I’d suggest you hire a programmer to do this.

    @ar_m

    Participant

    Thanks for your help in advance and your recent reply @robin-w.

    I would be happy if someone else could help me fix the problem.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar