0

When I create a new post of a Custom Post Type, my debug.log gets filled with PHP Notice: Unidentified index: warnings for each of the meta fields.

This question has been asked and answered before here and I implemented the solution there but each time I create a new post of this type, I am still seeing the errors in debug.log.

The code for the save method is as follows:

/** Listener for saving post */
public function save() {
    $post_type_name = $this->post_type_name;

    add_action( 'save_post',
       function () use ( $post_type_name ) {

            // Do not autosave meta box data
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
                return;
            }

            // Abort if the nonce field is not set
            if ( ! isset( $_POST['my-nonce'] ) ||
                 ! wp_verify_nonce( $_POST['my-nonce'], MY__PLUGIN_FILE ) ) {
                return;
            }

            global $post;

            if ( isset( $_POST ) && isset( $post->ID ) && get_post_type( $post->ID ) == $post_type_name ) {
                global $custom_fields;

                // Loop through all meta boxes
                foreach ( $custom_fields as $title => $fields ) {

                    // Loop through all fields in meta box
                    foreach ( $fields as $label => $type ) {

                        $field_name = self::uglify( $title ) . '_' . self::uglify( $label );

                        // Prevent PHP Warnings for undefined index
                        if ( isset( $_POST['fitcase'][ $field_name ] ) ) {
                            $metadata = $_POST['fitcase'][ $field_name ];
                        } else {
                            $metadata = null;
                        }

                        update_post_meta( $post->ID, $field_name, $metadata );
                     }
                 }
            }
        }   
    );
}
3
  • I assume you have set the nonce properly somewhere in your code, so what is the value shown when you echo $_POST['my-nonce']? I imagine it is NULL, but have you tried $_REQUEST instead of $_POST?
    – Gary D
    Commented Jan 5, 2018 at 5:45
  • @GaryD Yes. The nonce is set by an anonymous callback function in the add_meta_box() call in a different method. When I do a print_r() of $_POST I get an empty array and for $_REQUEST I just get an array with the post_type key set.
    – Cedon
    Commented Jan 5, 2018 at 18:12
  • @GaryD I actually found the source of the problem. It was in my method that constructs the fields for the meta boxes. I put in a check for isset($meta[ $field_id_name ]) and if it fails it sets $meta[ $field_id_name ][0] = null; and that cleared it.
    – Cedon
    Commented Jan 5, 2018 at 18:25

1 Answer 1

0

I dug deeper into the warnings and found that it was actually a different method in my class that was causing the error. I have one that creates fields within meta boxes and sets the value attribute to the content of $meta[ $field_id_name ][0]. This is what was causing the notice message.

So ahead of construction of the form element, I added the following:

// Check for meta data for value attribute and set to null if not found
if ( ! isset( $meta[ $field_id_name ] ) ) {
    $meta[ $field_id_name ][0] = null;
}

I ran it through a test with several loads of a new post and with one with pre-existing metadata and it works.

Your Answer

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

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