SumTips

Add, Delete and Edit Custom Fields in WordPress

WordPress provides authors with a way to add additional information to a post/page by using custom fields. Custom Fields can be anything from defining an author of a post to setting an expiry date.

In this post, I will show how can add, delete and edit custom fields in WordPress.

Add Custom Field to a WordPress Post/Page

It’s quite easy to add custom fields to a post/page, and you might have already done it many a times.

To do so, simply find the “Custom Fields” meta box, and add a unique field name and any value you want. For example, here I will add a new custom field value of “author” to a post, which has “SumTips Admin” as it’s value.

Delete Custom Field Value of a WordPress Post/Page

There are three ways to delete a Custom field from WordPress, and all are shown here.

Delete custom field from database:

Login to phpmyadmin, and click on wp_postmeta in the left sidebar. Next go to Structure view, select meta_key and click on browse.

Now you can see the earlier created author custom field.

However you cannot delete the value here.

So, now click on the Search tab, enter author in the meta_key field and click on “Go.”

This will give you a list of all the post which has author as a custom field.

Here select all posts and click on drop to delete the custom field. Another option is to, select only those post from which you want to remove a particular field from and drop them.

Delete custom field using plugin:

An easier way is to use a plugin called Delete Custom.

It gives you a simple interface to select and delete a Custom Field from a drop-down list.

Download: Custom Delete

Edit Custom Field of a WordPress Post/Page

To modify custom fields, follow the steps mentioned earlier, but instead of deleting this time click on “Change.”

That will bring you to a page as below:

Here change the value to anything you want and save it.

Hide Custom Field Metabox in WordPress Post/Page Edit Page

If you don’t use or don’t want the Custom Field Metabox, it can be easily removed by adding the following code to your functions.php file.

function remove_custom_meta_boxes() {
	remove_meta_box('postcustom','post','normal');
	remove_meta_box('postcustom','page','normal');
}
add_action('admin_init','hide_custom_meta_boxes');

Remove “post” or “page” line to keep the box in either.

Automatically Add a Specific Custom Field to all New WordPress Post/Page

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
	global $wpdb;
	if(!wp_is_post_revision($post_ID)) {
		add_post_meta($post_ID, 'field-name', 'custom value', true);
	}
}

Replace the field-name and custom value with your Custom Field Name, and Value. via WPCanyon