Get Wordpress Author Page Url With User ID

Here is another Wordpress tip for people who face problem finding the function to retrieves Wordpress author page url using a particular user id. I did find a lot of Wordpress functions that require them to be used within the loop. However, i need a particular function which takes in a user id as a parameter and provide the author page url to me outside the loop. I spend some time searching on Google but couldn’t find a decent page that provide me with this information (well, i found a lot of junk page around the internet nowadays though.). Hence, i went into the core and look for it myself. Lucky there was such function!

Wordpress Get Author Page Function

if you try to find the author core functions codes, it will lead you to wp-include/author-template.php file. Once you scan through the functions within the file, you will notice that there are only a few methods that take in parameters. Hence, its not that difficult to find. On line 197 you will notice the function get_author_posts_url as shown below,

function get_author_posts_url($author_id, $author_nicename = '') {
	global $wp_rewrite;
	$auth_ID = (int) $author_id;
	$link = $wp_rewrite->get_author_permastruct();

	if ( empty($link) ) {
		$file = get_option('home') . '/';
		$link = $file . '?author=' . $auth_ID;
	} else {
		if ( '' == $author_nicename ) {
			$user = get_userdata($author_id);
			if ( !empty($user->user_nicename) )
				$author_nicename = $user->user_nicename;
		}
		$link = str_replace('%author%', $author_nicename, $link);
		$link = get_option('home') . trailingslashit($link);
	}

	$link = apply_filters('author_link', $link, $author_id, $author_nicename);

	return $link;
}

And you can use this function this way

$curauth = get_userdata(get_query_var('author'));
$current_link = get_author_posts_url($curauth->ID,$curauth->display_name);

You can don’t provide the second parameter, display_name but it is advisable to do so as you will notice above that the core function will try to retrieve from the database if you omit the second parameter which also means an additional sql query which is not something we want :)

 

Like this post? Share it!

http://hungred.com/wp-content/plugins/sociofluid/images/digg_48.png http://hungred.com/wp-content/plugins/sociofluid/images/reddit_48.png http://hungred.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://hungred.com/wp-content/plugins/sociofluid/images/delicious_48.png http://hungred.com/wp-content/plugins/sociofluid/images/furl_48.png http://hungred.com/wp-content/plugins/sociofluid/images/technorati_48.png http://hungred.com/wp-content/plugins/sociofluid/images/google_48.png http://hungred.com/wp-content/plugins/sociofluid/images/myspace_48.png http://hungred.com/wp-content/plugins/sociofluid/images/facebook_48.png http://hungred.com/wp-content/plugins/sociofluid/images/twitter_48.png

Related posts:

  1. How To Exclude Page Permanently In Wordpress Without Using Any Plugin here is a way to permanently exclude pages globally across...
  2. “You do not have sufficient permissions to access this page.” on plugin admin page after update to Wordpress 3.0 A small fix for wordpress plugin developer after an upgrade...
  3. Disable Automatic Conversion Of URL To HTML Link In Wordpress Comment Disabling the automatic converting url or email addresses Wordpress sees...
  4. Multiple Wordpress installation sharing one user table multiple Wordpress installation with the same user table? This means...
  5. Customize Wordpress Avatar Using Filter Hook Another Wordpress tutorial that uses Wordpress filter hook to customize...

This entry was posted on Saturday, April 24th at 2:04 AM and is filed under How-to, Others. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

One User Commented

  1. It’s very good.
    I like this.
    Thanks for share.
    And I wrote something to introduce this project for my readers.
    You can find the post about this in my website.

Comments are automatic closed after certain period. Please send the author an email instead.