I am building a client portal where my clients can log in and see all their pending projects, completed projects and create new projects.
Though I’m stuck at the start.
The dropdown menu is called “My Projects”
If “User 1” is logged in and they click on “My Projects” then I need that link to link to:
mysite.com/client-portal/my-projects/(USERID or USERNAME)/
This will be their dedicated page so if user 1’s username is johndoe
the link will be my-projects/johndoe/
If user 2 (janedoe) is logged in. when they click “My Projects” they will be taken my-projects/janedoe/
and so on, so fourth.
So basically I need a %username%
permalink I can put into a custom menu link that will change %username%
with the current logged in user’s username.
Can this be done?
I think you can make this much easier than you are. Just create a
my-projects
page and forget about the custom rewrite. Why? Because a user will only need to see their own projects. No need for a just rewrite because you can get the current user any time.Embed the project rendering into a shortcode and just pop it into whatever page you want.
That said, if you really want that url, you’ll need to do a custom rewrite.
Then hook in someplace late (eg.
template_redirect
) and if you have the query var for the portal, render the customers projects.You might also be able to do something with
add_rewrite_tag
. A custom rewrite will mean you have to do some other stuff to make it work in menus. The shortcode approach above means you can use the menu system as normal.WordPress has a built in function for getting the current user:
My approach would then be to create a variable such as:
Then in your HTML just do this
<a href =userportal?<?php echo $userid ?> >menu <a>
Then just use the php GET variable to get the user id from the url and you can use that to get whatever details you need about the user.
That way instead of making a new page for every user you can use AJAX and request the portfolio of any user based on their ID. Much easier and more scaleable in the long run.