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~

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 🙂

Multiple Upload Using Single Upload File With jQuery

This is a trick to perform multiple upload using a single upload bar instead of multiple upload bars created by stickman. If you are looking for multiple upload using iframe or asynchronous method, you can visit this simplest way tutorial that explains how iframe method can be done and whether ajax method is available. I first came across stickman article was when i was doing research for multiple upload without having the file uploaded into the server. His idea was brilliant. However, i hardly have the need for such function until recently when my project submit form required multiple upload using a single upload bar again. Stickman method was pure JavaScript which is great and efficient but i needed something from jQuery. Here, i will try to simplify his method into jQuery form.

Concept of Single Multiple Upload File

In order to achieve single multiple upload file, we will have to understand that multiple upload is still possible even without asynchronous upload, that is, we will need multiple upload bar displaying on the screen and having user to click on each upload bar to upload multiple item at once. In stickman concept, its almost the same. The only differences is that he uses one upload bar and insert a new one on top of the existing upload bar AFTER a user has finished selected the item to be uploaded. Therefore it seems like there is only one upload bar. (the process is too fast for our eyes to follow. Thus, we all get trick when this happen) But in reality, all these upload bars were only hidden from user but not from codes. Once, the submit button is pressed, all the visible and non-visible upload bars are being uploaded to the server! This way we get our cheap multiple upload using a single upload bar method.

jQuery Single Multiple Upload File

I won't be displaying stickman code in this article as you can download directly from his website. We can now proceed with the coding after we have understand how stickman method works.

HTML code

All we need on the HTML section are these:

<div>
	<input type="file" name="files[]">
	<div id="upload_list"></div>
</div>

jQuery code

jQuery code is fairly simple. Let's don't complicate stuff. All we need to do is this.

jQuery(document).ready(function($){
var replaceMe = function(){
	var obj = $(this);
	$(obj).css({'position':'absolute','left':'-9999px','display':'none'}).parent().prepend('<input type="file" name="'+obj.attr('name')+'"/>')
	$('#upload_list').append('<div>'+obj.val()+'<input type="button" value="cancel"/><div>');
	$("input[type='file']").change(replaceMe);
	$("input[type='button']").click(function(){
		$(this).parent().remove();
		$(obj).remove();
	});
}
$("input[type='file']").change(replaceMe);
});

The above takes care of the basic. If you wish to restrict the number of upload, you can place a restriction variable.

jQuery(document).ready(function($){
var max = 2;
var replaceMe = function(){
	var obj = $(this);
	if($("input[type='file']").length > max)
	{
		alert('fail');
		obj.val("");
		return false;
	}
	$(obj).css({'position':'absolute','left':'-9999px','display':'none'}).parent().prepend('<input type="file" name="'+obj.attr('name')+'"/>')
	$('#upload_list').append('<div>'+obj.val()+'<input type="button" value="cancel"/><div>');
	$("input[type='file']").change(replaceMe);
	$("input[type='button']").click(function(){
		$(this).parent().remove();
		$(obj).remove();
		return false; //safari fixes
	});
}
$("input[type='file']").change(replaceMe);
});

Where max is the counter that restricts the number of uploads. Since jQuery takes care of most of the complicated things. The code eventually became smaller. For some reason bind doesn't works for the event change. Thus, i will have to attach the event again. From pure JavaScript of 156 line into a few line of codes, jQuery definitely makes developers life much more easier (doesn't means the code produce by jQuery is much more efficient though.).

The Demo

The demo can be found here.

Conclusion

The above jQuery code has simplify the version found on stickman site. Although the display doesn't looks as attractive but my objective has been achieved. Sometimes, JavaScript can be harder to read compared to a jQuery one. However, JavaScript version still has a better efficiency as compared to a jQuery one.