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~

Disable Automatic Conversion Of URL To HTML Link In WordPress Comment

As usual, i'm busy working on WordPress development and found something missing on the web (may be not much people needs it). So i dig into WordPress core for such information and write this down here in case someone find it useful. I found that WordPress will automatically convert all URL or Email addresses from string to HTML link for all comment text. This is good thing for some users but its not such a good thing for my project. Therefore, i wanted to disable it but couldn't find any resources around the web. So i decideto write this down in case someone like me who wished to disable WordPress automatic conversion of url addresses to html links on the comment text. It's pretty simple to disable this feature. You can remove this feature from WordPress by adding the following sentence to remove the automatic conversion.

			remove_filter('comment_text', 'make_clickable', 9);

Once this is added into your plugin or function.php file in your template, WordPress will not automatic convert all URL it sees to HTML link on the comment text. Have fun 🙂