2

I'm getting this error with my custom post metabox. The custom post and metabox works fine, but if i have debug on and when i try to save any other page or post, i will get an error:

Notice: Undefined index: at_nonce in /Users/jay/site/wp-content/themes/mytheme/dogs-post-type.php on line 53

Notice: Undefined index: at_nonce in /Users/jay/site/wp-content/themes/mytheme/dogs-post-type.php on line 53

Warning: Cannot modify header information - headers already sent by (output started at /Users/jay/site/wp-content/themes/mytheme/dogs-post-type.php:53) in /Users/jay/site/wp-includes/pluggable.php on line 876

My code:

 <?php

      // Add new custom post for dogs
      function example_dogs_custom_init() {
          $args = array( 
            'label' => 'Dogs',
            'public' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'has_archive' => true,
            'menu_position' => 5,
            'supports' => array('title', 'editor'),
            'rewrite' => array('slug' => 'pets/dogs', 'with_front' => false)
          );
          register_post_type( 'dogs', $args );
      }
      add_action( 'init', 'example_dogs_custom_init' );


      // METABOX breed
      add_action("add_meta_boxes", 'test_add_post_meta_boxes_breed');

      function test_add_post_meta_boxes_breed() {
        add_meta_box(
        'breed-meta',      // Unique ID
        'Breed',   // Title
        'breed_cb',   // Callback function
        'dogs',         // Admin page (or post type)
        'side',         // Context
        'default'         // Priority
        );
      }

      function breed_cb() {
        global $post;
        $breed_input = get_post_meta($post->ID, 'breed_field', true);

        // unique identifier, name of hidden field
        wp_nonce_field(__FILE__, 'at_nonce');
        ?>
        <label for="breed_field">Breed:</label>
        <input type="text" name="breed_field" id="breed_field" value="<?php echo $breed_input; ?>" />
        <br><span></span><?php
      }

      add_action('save_post', function() {
        global $post;
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
        // security check - nonce

        // verify this came from the our screen and with proper authorizarion,
        // because save_post can be triggered at other times
        if ( $_POST && !wp_verify_nonce($_POST['at_nonce'], __FILE__) ) {
            return;
        }

        if ( isset($_POST['breed_field']) ) {
              update_post_meta($post->ID, 'breed_field', $_POST['breed_field']);
        }

      });

    ?>

There is something wrong with the nonce field but i cant figure out what. If it helps, the code structure is from Jeffrey Ways The Magic of WordPress Custom Post Types course.

2 Answers 2

8

These are your problem lines:

if ( $_POST && !wp_verify_nonce($_POST['at_nonce'], __FILE__) ) {
    return;
}

You check to see that $_POST is set, but you don't check $_POST['at_nonce']. If $_POST is set but that key is not then you will get a Notice. It is a simple fix:

if ( isset($_POST['at_nonce']) && !wp_verify_nonce($_POST['at_nonce'], __FILE__) ) {
    return;
}

When that Notice prints, content is sent to the browser. Since that content is being sent before the proper headers are sent, when those proper headers are sent you get the "Cannot modify header information" warning.

1
  • This one fixed the issue, thanks s_ha_dum. There is a missing ] after 'at_nonce' on your answer, otherwise it worked fine.
    – jayjay
    Mar 19, 2013 at 22:24
0

at_nonce doesn't exist when saving any other post type - hence the notice. You should only check the nonce if you already have the right post type. Try replacing this

if ( $_POST && !wp_verify_nonce($_POST['at_nonce'], __FILE__) ) {
    return;
}

if ( isset($_POST['breed_field']) ) {
      update_post_meta($post->ID, 'breed_field', $_POST['breed_field']);
}

with this (untested)

   if ( isset( $_POST['dogs'] ) && wp_verify_nonce( $_POST['at_nonce'], __FILE__  ) ) {
        update_post_meta($post->ID, 'breed_field', $_POST['breed_field']););
    }
1
  • 1
    Please: Always use the esc_*() functions when updating data in the DB.
    – kaiser
    Mar 19, 2013 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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