WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieving the current user location as a URL? (5 posts)

  1. Scoodle
    Member
    Posted 2 years ago #

    Hi,

    While creating some log in / log out links for my theme, I've run into a problem. I'd like to return the user to where he or she was on my site after he or she logs in or out, and fortunately this can be achieved using wp-login.php?redirect_to=http://example.com/ for logging in and wp_logout_url('http://example.com') for logging out.

    I cannot, however, seem to find a good way of retrieving the URL to return the users to. the_permalink() will only return post and page links, while get_category_link() will only return category links. Is there a more universal way of getting a link to the current user location?

    If not, I could of course switch between the methods using a conditional. I'd have to determine whether the user is browsing a post/page or a category, in that case.

    What should I do?

    Any advice is appreciated!

  2. jdavhar
    Member
    Posted 2 years ago #

  3. Scoodle
    Member
    Posted 2 years ago #

    Thank you for your reply, but unlike the other OP, I know how to redirect. I just don't know what function I could use to retrieve the URL of the page to redirect to. Serving $_SERVER[HTTP_REFERER] as the redirect_to argument for wp-login doesn't seem like a good idea.

  4. jdavhar
    Member
    Posted 2 years ago #

    http://wordpress.org/extend/plugins/custom-login-redirect/

    http://www.longren.org/wordpress-tip-redirect-to-previous-page-after-login/

    if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
    	add_filter('login_redirect', 'my_login_redirect', 10, 3);
    	function my_login_redirect() {
    		$location = $_SERVER['HTTP_REFERER'];
    		wp_safe_redirect($location);
    		exit();
    	}
    }
  5. Scoodle
    Member
    Posted 2 years ago #

    Not the most elegenat solution, but it'll have to do. Thank you for your efforts.

Topic Closed

This topic has been closed to new replies.

About this Topic