WP Plugins and Custom Fields

Date: 5th November 2007 at 9:14 pm | Filed under: development, scripts, wordpress | Author: Sam Burdge

This article is intended to address issues involving the use of add_post_meta and delete_post_meta in WordPress plugin design. These functions can be used to add or delete custom fields for a post, useful for plugin design as the value for the custom field can be entered via a form in the write page. The add_post_meta code would look something like this:

function add_custom_field($id) {
//Get the value from your form
$userEnteredValue = $_POST["userEnteredValue"];
//Add your value as a custom field with the key 'myKey'
add_post_meta($id, 'myKey', $userEnteredValue);
}

This function is then hooked into wordpress by adding it to the following actions:

add_action('edit_post', 'add_custom_field');
add_action('publish_post', 'add_custom_field');
add_action('save_post', 'add_custom_field');
add_action('edit_page_form', 'add_custom_field');

This means that every time a post or page is edited, saved or published a new custom field is created. Obviously this is a problem, as we don't want more than one custom field. Also, what if the user has left the form value blank? This is where delete_post_meta comes in. First we need to delete the existing custom field to ensure that it only appears once, then we need to write the new custom field data if the form field is not blank. The function would now look like this:

function add_custom_field($id) {
//delete the existing field
delete_post_meta($id, 'myKey');
//Get the value from your form
$userEnteredValue = $_POST["userEnteredValue"];
//Add your value as a custom field with the key 'myKey'
//but only if the form data exists
if (isset($userEnteredValue) && !empty ($userEnteredValue)){
add_post_meta($id, 'myKey', $userEnteredValue);}
}

Ok, so this seems to solve the problem. When you test your function by entering values into the form you will see that only one custom field is created at a time, and that if the form field is left blank no custom field will be created. However, there is now another problem relating to the way the function is hooked into wordpress. What happens to the custom field if the post is saved from outside of the write / edit page, for example when a comment is submitted? The form input value will be empty, and therefore the custom field will not be written. So far I have come up with two solutions for this, the first is to have a value such as 'delete' or 'blank' that can be entered into the form in order to delete the custom field, but if the form is left empty the field will be preserved. The second and generally better idea is to pass an extra hidden value with your form that your plugin can then use to check if the form has been submitted.

5 Responses to “WP Plugins and Custom Fields”

  • Comment by johnbillion
    Date: December 6th, 2007 at 5:45 pm

    Erm, have you tried update_post_meta ?

  • Comment by Sam Burdge
    Date: December 10th, 2007 at 2:20 pm

    Hi Johnbillion,
    update_post_meta is a tidier way, true. But the problem still remains that if you hook it in using add_action as stated above, there will be instances where the function is called even though the form hasn’t been submitted (i.e. the post/page hasn’t been edited), like when a comment is approved.
    Sam

  • Comment by WP Plugins and Custom Fields | Sam Burdge | Squico
    Date: August 19th, 2009 at 4:40 am

    [...] In: Wordpress plugins 19 Aug 2009 Go to Source [...]

  • Comment by Gvnah
    Date: January 21st, 2010 at 9:29 pm

    hi, i hope this post is still being operated. I’m trying to add a custom field value to all my old posts.. (i want the old posts to have the same value though [same thumbnail])

    I just re-designed my wordpress blog’s theme and the new theme uses custom value to insert thumb-nails..

    the problem is that.. i already have old posts that i want to insert the thumbnails

    could anyone help PLEASE :)

  • Comment by David
    Date: November 10th, 2010 at 7:08 am

    This page is really tough to read with the intense contrast. You might want to make the white a little less bright. Good blog though!

Leave a Comment