Skip to main content

Dynamic Url for custom menu item "My Spaces" in wordpress menu

Hi can anyone suggest what url can I paste for the custom menu item "My Spaces" in the wordpress menu setting of my theme so that when any users click on "My Spaces" it would direct them to their list of spaces.

Currently, dynamic url is not available. We will add that in the next version.

arjun arjun

Shahjahan JewelΒ Thank you for your info about it being added in the next version.

But if you want to redirect to the self profile then you can use this:
COMMUNITY_URL/u

This will redirect the current user's profile.

Thomas Oates

Code like this may work. It was generated by ChatGPT so use at your own risk.

Add the following actual text to the menu item: /u/username/spaces
If your community is at /portal or some other path, add that before the /u.

add_filter('wp_nav_menu_objects', 'replace_username_in_menu_urls');

function replace_username_in_menu_urls($items) {
    // Only proceed if user is logged in
    if (!is_user_logged_in()) {
        return $items;
    }

    $current_user = wp_get_current_user();
    $username = $current_user->user_login;

    foreach ($items as $item) {
        // Check if the URL contains the placeholder
        if (strpos($item->url, '/u/username/spaces') !== false) {
            $item->url = str_replace('/u/username/spaces', "/u/{$username}/spaces", $item->url);
        }
    }

    return $items;
}

arjun arjun

Thomas OatesΒ Thank you so much for your snippet. As always you have been very helpful.