Question

I have several author page like

mysite.com/author/username1
mysite.com/author/username2

I want to create a dynamic url like

mysite.com/author/current_loggedin_username

How to create this.

Was it helpful?

Solution

First, you should check if the user is logged in - use is_user_logged_in().
Next step is to get ID of current user with get_current_user_id().
Next, get the URL for the author with the given ID using get_author_posts_url().
The last step is to display the button with the link.

if ( is_user_logged_in() )
{
    $uid = get_current_user_id();
    $link = get_author_posts_url( $uid );
    printf( '<div><a href="%s">My posts</a></div>', $link );
}

You can insert such a code in the header.php file or add it as a function to functions.php file and call this function in the header.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top