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.

 

WordPress Template_page meta key and update_post_meta

This post will be a fairly simple and straight forward one. It has been a long time since i wrote anything on Hungred Dot Com. I really like to write more regarding my experiences during each of my project and some useful designing list that i would like to keep but i just can't seems to get the time. Nonetheless, i would still write such article that i couldn't find on Google to share with people who needs it.

Template_page Meta Key

Since 9 of January 2009, WordPress has removed template_page key from wp_insert_post from the table wp_post to wp_meta_post. Therefore, some of you who are looking for this particular key to insert customize template will find difficulty as there are no documentation of any kind that allows you to work this out peacefully. The changes will require user to use update_post_meta in order to interact with wp_meta_post table. Hence, our main concern here is the exact key used to update any particular post/page template using the method update_post_meta. I dig around and found out that WordPress no longer uses Template_page as the key but uses _wp_page_template as the key for our meta post table. Hence, you will write something like this in order to update a particular post/page template.

update_post_meta($id, "_wp_page_template", "new_template.php");

The third parameter will require you to place in the exact file name plus extension. Without extension but pure name won't work. Once this sentence is being executed, your post which has the id in the variable $id will have new_template.php as its own template! Enjoy!

How To Exclude Page Permanently In WordPress Without Using Any Plugin

Excluding page in WordPress is fairly easily when we were given some particular function in WordPress to list out pages  such as wp_list_pages or get_pages where these methods all gives us certain parameter for us to exclude certain  pages that we do not wish to display. And these methods are usually used on WordPress theme although occasional we would put them into WordPress plugin for other reason. However, using these methods will only exclude page on the THAT particular function call which really doesn't suit my need. I want something permanent which excludes every page whenever i use those methods. If you are like me who do not wish to use plugin such as the Exclude Pages plugin by Simon for whatever reason you have (mine was because of efficiency and customization wise), this may be an article that will definitely help you.

Excluding Page Permanently In WordPress

Anything that you wished to permanently done on WordPress will usually associate closely with action or filter hook in WordPress. In this case where we want to exclude page permanently, we will use filter hook.

add_filter('wp_list_pages_excludes', array($this, 'page_filter'));
function page_filter($exclude_array) {
	global $wpdb;
	$table = $wpdb->prefix . "posts";
	$sql = "SELECT ID FROM ".$table." WHERE post_title ='Exclude' OR post_title ='This' OR post_title ='Pages' OR post_title ='Search'";
	$id_array = $wpdb->get_col($sql);
	$exclude_array=array_merge($id_array, $exclude_array);
	return $exclude_array;
}

The method we will be using on our filter would be wp_list_pages_excludes which helps to exclude pages. For my case, i wanted to exclude some of the page in WordPress and when i said some i meant more than one. Hence, i wrote my own sql query and retrieve as an array of id's. This way, we can merge with the one that you or anyone who has already excluded and return to the core function to deal with it. (this way we reduce the number of sql query needed to retrieve the same amount of id's, if we use get_post method there will be 4 additional query instead of one.)

For people who are not familiar to what i am talking about, you can just copy and paste the above code into your function.php file in your theme and change post_title = 'Exclude' and post_title etc. to your own page name instead of Exclude, This and Pages. Cheers~