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 🙂

Customize WordPress Avatar Using Filter Hook

WordPress have an excellent feature for providing individual thumbnail within your WordPress blog. However, most of us are aware that most users who have a customize thumbnail image are usually those who sign up with Gravatar and the rest of the user will only have their default image provided by WordPress. Some of us will want a customize WordPress Avatar for our logged in user or customize the default WordPress image rather than using the default one. This is possible using filter hook that is currently not documented in WordPress to provide a customize avatar for individual logged in user. On the other hand, default image can also be overwrite so that you can have your own set of avatar uniquely for your WordPress blog.

Default WordPress Avatar

Rather than using WordPress default avatar, there is an easy way to customize the default WordPress Avatar even without using any filter hook. Here is how you do it.

<?php echo get_avatar( $comment->comment_author_email, 100, get_template_directory_uri()."/images/face.jpg" ); ?>

Notice that get_avatar is the recommended way to get avatar from WordPress and this is usually located in your WordPress theme. This function allows you to change the default WordPress avatar by providing a customize path to the default WordPress avatar. You can also create your own set of random avatar using this method. (either using another table in your database or just use the php method random to randomly select an image)

Customize WordPress Avatar With Filter Hook

Another way of customizing WordPress Avatar is to use a filter hook provided by WordPress. I found this accidentally while customizing WordPress for one of my project needs. The filter hook that was given within the method of get_avatar was this.

			add_filter('get_avatar', array($this,'site_get_avatar'), 10, 5);

Notice that the new filter was get_avatar which is also the method given in WordPress to display any avatar. Take note that i also provide the third and fourth parameter. The fourth parameter is important so that our method site_get_avatar have the appropriate value for us to work with, without the fourth parameter, you won't be able to retrieve other information other than the avatar given by WordPress itself. You will understand this better after i have provided the method site_get_avatar as shown below,

function site_get_avatar($avatar, $id_or_email, $size, $default, $alt){
	//$avatar format includes the tag <img>
	$imgpath = "http://website.com/image/myownimage.jpg";
	$my_avatar = "<img src='".$path."' alt='".$alt."' height='".$size."' width='".$size."' />";
	return $my_avatar;
}

The above method contains 5 parameters which are describe below,

  1. $avatar: the avatar given by WordPress with the tag . This is not the path of the default image.
  2. $id_or_email: this is the email or id of the user
  3. $size: size of the image
  4. $default: this should be the default image provided by WordPress or the path given by us when we used get_avatar (third parameter as shown previously)
  5. $alt: this is the alt value given in your img tag.

Previously, i mention that the fourth parameter in the filter hook is important right? The reason is that if the fourth parameter is not provided in your filter hook, the value for number 2 onwards will not be provided by WordPress to your custom method, site_get_avatar. You will only get the value $avatar in default. Hence, the fourth parameter is important 🙂

WordPress Customize Avatar

Depending on what you need, this action hook is actually quite useful for customizing WordPress avatar. You can abandon this hook if you just want to customize the default image provided by WordPress. However, if you wish to customize each user avatar without using Gravatar, you will definitely find this pretty useful! Good luck!

Change WordPress Default From Email Address Without Plugin

In WordPress, there is this default WordPress email, [email protected] where ALL WordPress installation will have. This default email is used on all your WordPress blog From address whenever you or your user receives an email from your WordPress blog. You will also notice that WordPress default email name is 'Wordpress' too instead of your own WordPress blog name. Something like the image shown below,

Well, having a default wordpress email address and name wasn't that bad but sometimes you would like to place it with your own branding name so that your users know its you instead of WordPress or something that is not associated with WordPress. If you search online, you will definitely find some ready made plugin available on the market but you might not want them due to various result such as efficiency, customization and etc. Furthermore, this is really easy to achieve and doesn't really required any plugin that might affect your WordPress blog performance.

WordPress Email Hook

The main thing we need to be aware of are the hooks needed to change the default WordPress From email addresses. If you dig into WordPress wp_mail function, you can easily notice there are two hooks that allow you to do this.

<pre class="brush: php; title: ; notranslate" title=""><br />add_filter('wp_mail_from','_fromMail');<br />add_filter('wp_mail_from_name','_fromMailName');<br />

The above are the two filter hooks that are required to change your default WordPress from email address.

 

WordPress Email Hook Functions

So we have our hooks, the next thing is to provide the function to change the name of the default email from address.

<pre class="brush: php; title: ; notranslate" title=""><br /><%%KEEPWHITESPACE%%>		function _fromMail($email){<br /><%%KEEPWHITESPACE%%>			return "[email protected]";<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		function _fromMailName($name){<br /><%%KEEPWHITESPACE%%>			return "Go Where Eat";<br /><%%KEEPWHITESPACE%%>		}<br />

It's pretty simple to understand from the above code, we are just passing in our name and email to overwrite the one given by WordPress.

 

Change WordPress Default From Email

For those who are not familiar with PHP coding and couldn't understand what the above means, all you need to do is to copy the below code to either a new plugin file or your function.php file in your theme folder and change the name and email address to the one you prefer.

<pre class="brush: php; title: ; notranslate" title=""><br />add_filter('wp_mail_from','_fromMail');<br />add_filter('wp_mail_from_name','_fromMailName');<br /><%%KEEPWHITESPACE%%>		function _fromMail($email){<br /><%%KEEPWHITESPACE%%>			return "[email protected]";<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		function _fromMailName($name){<br /><%%KEEPWHITESPACE%%>			return "Go Where Eat";<br /><%%KEEPWHITESPACE%%>		}<br />

For coders, this should be a piece of cake for you.

Â