• Hi guys!

    I wanted to know how can I redirect users to a page using an anchor after a successful login?

    Example:

    The user tried to access: mywebsite.com/my-article#subject-3
    He will be redirected to the login page before he can see the page.
    After his successful login, he will be redirected to /my-article instead of the first URL he tries to reach /my-article#subject-3

    How can I redirect users to the URL they try to reach without losing the anchor?

    I saw this but I don’t know how to use it in my case: https://github.com/kevinvess/wp-force-login/wiki/Whitelist-Dynamic-URLs

    Thanks in advance for your support

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Kevin Vess

    (@kevinvess)

    Hi– thanks for using Force Login!

    How can I redirect users to the URL they try to reach without losing the anchor?

    Unfortunately, it’s not possible to know the anchor since the hash (the string including the #) never gets passed to the server, it is solely a behavioral property of the browser.

    You can use Javascript to get the hash value, but then the page has loaded to run Javascript instead of having the PHP (server-side) redirect visitors to the login screen.

    However, you may change the url passed to the ?redirect_to= query using the v_forcelogin_redirect filter. Below is an example of sending visitors trying to visit your “/my-article” page to a specific anchor.

    
    /**
     * Set the URL to redirect to on login.
     *
     * @return string $redirect_url URL to redirect to on login. Must be absolute.
     */
    function my_forcelogin_redirect( $redirect_url ) {
      if ( is_page('my-article') ) {
        $redirect_url = site_url( '/my-article/#subject-3' );
      }
      return $redirect_url;
    }
    add_filter( 'v_forcelogin_redirect', 'my_forcelogin_redirect' );
    Thread Starter Nasser-eddine BENAZIZA

    (@devagency)

    Hi Kevin,

    Thanks for your answer!

    If I understand, the method you propose me is for only one page with an anchor? If I have multiple pages with anchor, I can’t use this solution because every redirection will be on the page I’ve entered in this function?

    Thanks anyway for your help 🙂

    Plugin Author Kevin Vess

    (@kevinvess)

    The code example using the v_forcelogin_redirect filter only changes the redirect URL to an anchor link for that one page, is_page('my-article'), else it defaults to just the page url they tried to visit.

    Examples:

    If a visitor tried to access /another-page/ they would be redirected to /another-page/ after a successful login.

    If a visitor tried to access /my-article/#subject-1 they would be redirected to /my-article/#subject-3 after a successful login.

    The code example using the v_forcelogin_redirect filter shows you how you could alter the redirect URL, but like I said before– it’s not possible to know the anchor since the hash (the string including the #) never gets passed to the server.

    Thread Starter Nasser-eddine BENAZIZA

    (@devagency)

    Thanks Kevin, everything is crystal clear now 🙂

    Plugin Author Kevin Vess

    (@kevinvess)

    No problem, thanks for using Force Login! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redirect users to URL with anchor’ is closed to new replies.