WP Plugins and Custom Fields
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.






Erm, have you tried update_post_meta ?
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