J.me

WordPress Snippets part 1

Well, this will be a short list of WordPress snippets that we could use. This snippets is collected from my daily work on WordPress and should be useful for daily basis.

WordPress mail snippets

WordPress has its own improved function for sending email, it is wp_mail. We should always use this function if we wanted to send email within WordPress. With this function, we don’t need to worry much about the mail header and stuff. More over, there is plenty of filter we can use to customize it.

Set content type to HTML

We can send HTML email by set the content type of email sent by WordPress. To do this, add this function to functions.php in your theme file.

1
2
3
4
5
function set_email_html_type( $content )
{
	return 'text/html';
}
add_filter('wp_mail_content_type', 'set_email_html_type');

Set mail sender name

This function will set the mail sender name.

1
2
3
4
5
function set_mail_from_name( $name )
{
	return "Jeffri Hong";
}
add_filter('wp_mail_from_name', 'set_mail_from_name');

We could improve it by automatically set it to the first administrator name.

1
2
3
4
5
6
7
8
9
10
11
12
13
function set_mail_from_name( $name )
{
	$admins = get_users(array('role' => 'administrator'));
	if ( $admins )
	{
		$first_name = get_user_meta($admins[0]->ID, 'first_name', true);
		$last_name = get_user_meta($admins[0]->ID, 'last_name', true);
		if ( $first_name || $last_name )
			return "$first_name $last_name";
	}
	return "Jeffri Hong"; // the default value when it is failed to retrieve first/last name
}
add_filter('wp_mail_from_name', 'set_mail_from_name');

Set mail sender email

This function will set the mail sender email address.

1
2
3
4
5
function set_mail_from( $email )
{
	return "info@jeffri.net";
}
add_filter('wp_mail_from', 'set_mail_from');

As with the previous one, it is also possible to automatically set it to the first administrator email.

1
2
3
4
5
6
7
8
9
10
11
function set_mail_from( $email )
{
	$admins = get_users(array('role' => 'administrator'));
	if ( $admins )
	{
		if ( $admins[0]->user_email )
			return $admins[0]->user_email;
	}
	return "info@jeffri.net";
}
add_filter('wp_mail_from', 'set_mail_from');

Get post meta ordered by id

Getting a post meta, especially an array, can some time return an unexpected result. The problem is the get_post_meta function rely on the default SQL order. In few of my job, I need to use multiple post meta and keep it in order with each order, and it won’t work correctly unless we add the ORDER BY clause to the query. Unfortunately, the metadata functions didn’t have many filters we can work with. However, there is a filter we can use to override the function return value.

1
2
3
4
5
6
7
8
9
10
function filter_post_meta( $null, $object_id, $meta_key, $single )
{
	global $wpdb;
	$results = $wpdb->get_results("SELECT m.meta_value FROM $wpdb->postmeta m WHERE m.meta_key = '$meta_key' AND m.post_id = '$object_id' ORDER BY m.meta_id");
	$ret = array();
	foreach ( $results as $result )
		$ret[] = $result->meta_value;
	return $ret;
}
add_filter('get_post_metadata', 'filter_post_meta', 10, 4);

With this, every call of get_post_meta will be filtered by this function and return all post meta filtered by meta_id. The good news is, this filter run on top of get_metadata function and returned it immediately when there is a value.

Add post slug as CSS class to page lists

Often, we need to have a more definitive and unique class for page lists generated by function like wp_list_pages and other. By default, there is unique class for the page ID, but since page ID is generated automatically, this value vary on site to site – for example when you have a development version and live version of a site, you will need an alternative for unique class other than ID. Post slug is the better solution in this case, since it is defined by us and unique.

1
2
3
4
5
6
function add_page_css_class( $classes, $page )
{
	$classes[] = 'page-'.$page->post_name;
	return $classes;
}
add_filter('page_css_class', 'add_page_css_class', 10, 2);

Add this function to functions.php of your themes. Now, you will have one more class selector for your CSS.

And more to come

That’s it folks for the part 1. More will come in near future as I still have many interesting case I experienced and a nice solution that is re-usable anywhere. 🙂

Hope that useful for you.

No comment yet

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.