WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to add custom fields on post publish with this code which I added to my functions.php :

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);

This code should work but it doesn't ? I updated field and custom value but custom fields are never created.Why ?

share|improve this question
You have a period in the second add_action – AlxVallejo Aug 23 '12 at 14:07
Thank you so much ! :)))) It is fixed – Rorkkkk Aug 23 '12 at 14:46
@AlxVallejo please post your comment as an answer so it can be accepted to prevent this question from appearing as unanswered. – Milo Aug 23 '12 at 15:11
up vote 0 down vote accepted
add_action('publish_post'. 'add_custom_field_automatically');

You have a period in the second add_action. This should be a comma :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.