• I am trying to understand something that is probably trivial, since I cannot find suggestions anywhere. I am building a site (not yet online): From the front page, the user must log in, and if credentials are ok she is redirected to a page related to the group to which she belongs. In this page, there are the general info of the group; if needed, from this page the user may access her personal private page. The problem is here: since the user is already logged in, the link should take her to something like …mydomain-xcxxxx-privatepage-user01, whereas in the same situation user02 will be taken to ..-mydomain-xcxxxx-privatepage-user02. (- = /, prevent akismet…)
    Is it possible to “parametrize” the url with the variable “user name”?
    In a forum I found the answer to a similar question, with a snippet of code

    $current_user = wp_get_current_user();
    if ( is_user_logged_in() ) {
        wp_redirect( 'http://www.domain.com/author/'.$current_user->user_login.'/achievements/');  
        exit;
    } else {
        echo 'You have no business being here';
    }

    but I don’t understand how this can be applied to my case.
    Thanks for your attention to a newbie.

    • This topic was modified 4 years, 10 months ago by Jan Dembowski.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,
    I think the above code can approach what you want, just change it like below:

    
    wp_redirect( 'http://<your_domain>/<path>/privatepage/'.$current_user->user_login);  
    

    *The property “user_login” means user name.
    ref: document

    • This reply was modified 4 years, 10 months ago by quentin1995.
    Moderator bcworkz

    (@bcworkz)

    Creating such an URL doesn’t mean WP knows how to handle such a request. It would need a related rewrite rule that tells it how to use the elements of the URL. WP doesn’t really need to know the current user by URL parameter. WP knows the current user by their auth cookie. A page’s template could be coded to only display the current user’s personal content even though every user uses the exact same URL.

    For example, this is how cart links work on e-commerce sites. Everyone views their cart at example.com/cart/, but each user will only see their own cart items.

    Thread Starter roberto21

    (@roberto21)

    Thanks, but still I don’t understand how to proceed in practice. Maybe it is obvious, but I am really trying to learn If I am on the General page for the group, I was imagining something like “press HERE to get to your personal page”, with “HERE” containing the url to that page, but what url should I write there?

    Also, bcworkz, can’t find your cart example in the post

    Moderator bcworkz

    (@bcworkz)

    There’s no specific cart example in my earlier post. If you need a specific example, go to Amazon, log in, and add an item to your cart. Go to the cart page to see the item. Notice the cart URL is /cart/view.html?ref_=nav_cart. All users get the same URL. What the page shows is unique to each user because we all have different login cookies.

    Amazon is not a WP site, but the principle is the same. As long as the user is logged in, the same page URL can display unique content for that specific user. This is done by qualifying the query for content with user ID. A cart query would be for all cart items added by the current user. The query’s coding ensure’s no one could see (except admins) any other user’s cart items.

    The “Press HERE” link you’re imagining can go to the exact same page for everyone, yet each person would only see their unique content because the related query would be qualified with the current user’s ID. Having a unique URL for every user like you imagine could actually introduce a privacy leak if a third party were able to correctly guess another user’s URL. Of course code could be added to prevent this. The point being that while a unique URL is possible, it’s not optimal since the needed user information is already in their auth cookie anyway. Site cookies are sent with every HTML request, so if they’re logged in, we always know who made the request, regardless of the URL requested. We can then tailor content accordingly.

    Thread Starter roberto21

    (@roberto21)

    Yes, the logic is clear, the implementation (I am a newbie, or rookie, whatever) a little less clear. I was not thinking of a single url for all users, but to the possibility of using a “parametrized” url, like mydomain-privatepage-[variable]username[/variable], where username is replaced by the name of the user logged in. There is a separate page for each user; if the page can be accessed by that user only, a “smart” user cannot see the page, also knowing the username. Now, the url “syntax” above does not work in the link field of “HERE”, so what is (if any) the trick to make it work? I know this sounds like a silly question…
    By the way, the above syntax [variable]username[/variable] should work according to documentation in PeterLogin plugin. Probably I misunderstood that also.
    Thanks for your patience….

    Moderator bcworkz

    (@bcworkz)

    OK, you can do that if you want. It ignores the advantages of a CMS system for a more stilted, rigid approach, but it’s possible. If a user name is included in a page’s slug and there is such a page for every user, simply include the username in the slug when creating the page. It’s important the remaining, common part (“mydomain-privatepage-“) of the slug always be correct. Make the user the author of the page and publish the page as private to keep other users from accessing the page.

    If you want to give the current user a link to their particular private page, you need to first get their user object, then their username from that. Then concatenate it to the rest of the URL, like so:

    <?php
    $user = wp_get_current_user();
    if ( 0 != $user->ID ) {
      $url = site_url("/mydomain-privatepage-{$user->user_login}/");
      echo "<a href=\"$url\">View your user page</a>";
    } else {
      echo 'Log in to see a link to your user page';
    }
    ?>

    I’m not sure why you’d want “mydomain” in the page slug since it’s in the URL anyway. It’s more to get wrong when creating the page. IMO you should keep it simple, a slug more like “user-bcworkz”. Then a complete URL would be more like https://mydomain.com/user-bcworkz/

    You know, WP already has a built-in variation of this: https://mydomain.com/author/bcworkz/
    It leads to a page showing the user’s bio and all posts they’ve authored. It only works for users who have published a post, but admins can publish one for them in place of making a dedicated page like you intend. The main difference is this page is public. But you could alter author.php template code to only show content if the page is for the current user.

    Thread Starter roberto21

    (@roberto21)

    quentin1995: I must be missing something basic here. How does WP know which url must be redirected? Doesn’t wp_redirect expect two parameters, e.g
    wp_rediredct (old_url, new_url)
    If this is so (and I am not sure about this), might I be able to have something like

    wp_redirect('http://your_domain/privatepage/present_user', 'http://<your_domain>/<path>/privatepage/'.$current_user->user_login);

    and use http://your_domain/privatepage/present_user in the “common” link field?

    • This reply was modified 4 years, 10 months ago by roberto21.
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘variable (“parametric”) urls’ is closed to new replies.