Menu Link Redirect Based on Username or User ID

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.

Read More

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?

Related posts

2 comments

  1. 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.

    add_action('init', 'wpse105306_add_shortcode');
    function wpse105306_add_shortcode()
    {
        add_shortcode('wpse105306_projects', 'wpse105306_projects');
    }
    
    function wpse105306_projects()
    {
        $user = wp_get_current_user();
    
        $projects = wpse105306_get_projects_somehow($user);
    
        // do stuff with $projects
    }
    
    function wpse105306_get_projects_somehow($user)
    {
        // whatever you need here
    }
    

    That said, if you really want that url, you’ll need to do a custom rewrite.

    // make the rewrite work
    add_action('init', 'wpse105306_add_rewrite');
    function wpse105306_add_rewrite()
    {
        add_rewrite_rule(
            '^client-portal/my-projects/([^/]+)/?$',
            'index.php?wpse105306_portal=$matches[1]',
            'top'
        );
    }
    
    // make sure WordPress doesn't eat the wpse105306_portal query var
    add_filter('query_vars', 'wpse105306_add_var');
    function wpse105306_add_var($vars)
    {
        $vars[] = 'wpse105306_portal';
        return $vars
    }
    

    Then hook in someplace late (eg. template_redirect) and if you have the query var for the portal, render the customers projects.

    add_action('template_redirect', 'wpse105306_catch_portal');
    function wpse105306_catch_portal()
    {
        $username = get_query_var('wpse105306_portal');
        if (!$username) {
            return; // things go on as normal
        }
    
        $projects = wpse105306_get_projects_somehow($username);
    
        // do stuff with $projects
    }
    
    function wpse105306_get_projects_somehow($username)
    {
        // whatever you need here
    }
    

    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.

  2. WordPress has a built in function for getting the current user:

    <?php wp_get_current_user(); ?>
    

    My approach would then be to create a variable such as:

    <?php $userid = wp_get_current_user(); ?>
    

    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.

Comments are closed.