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.