Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a problem correcting saving metabox data on a custom post type. I thought I fixed it by applying the advice on this post: Metabox nonce PHP notice

which does get rid of the warning but leaves me unable to update the metaboxes. Here is the code in question :

function save_goal_info_meta( $post_id ) {
    global $goal_info_meta_fields;

    // verify nonce
    if ( ! isset( $_POST['product_noncename'] ) || ! wp_verify_nonce( $_POST['goal_info_meta_box_nonce'], basename( __file__ ) ) ) {
        return $post_id;
    }

    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }

    // loop through fields and save the data
    foreach ( $goal_info_meta_fields as $field ) {
        if ( $field['type'] == 'tax_select' )
            continue;

        $old = get_post_meta( $post_id, $field['id'], true );
        $new = $_POST[$field['id']];

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
    }
}

add_action( 'save_post', 'save_goal_info_meta' );

I understand more or less why it isn't updating but I can't seem to fix it without having an "Undefined Index".

$goal_info_meta_fields = array(

   array(
    'label' => 'Home Goals by:',
    'desc' => 'Add and select home scorers',
    'id' => $prefix.'home-scorers',
    'type' => 'home_player_list'
   ),
   array(
    'label' => 'Away Goals by:',
    'desc' => 'Add and select away scorers',
    'id' => $prefix.'away-scorers',
    'type' => 'away_player_list'
   )
);

Here is the output :

function show_goal_info_meta_box($field, $meta, $items = null) {
    global $goal_info_meta_fields, $post;
// Use nonce for verification

echo '<input type="hidden" name="goal_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

// Begin the field table and loop
echo '<table class="admin-table">';
foreach ($goal_info_meta_fields as $field) {
    // get value of this field if it exists for this post
    $meta = get_post_meta($post->ID, $field['id'], true);
    // begin a table row with
    echo '<tr>
            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
            <td>';
    switch($field['type']) {


        // home_player_list
    case 'home_player_list':
        echo '<a class="repeatable-add button" href="#">+</a> <span class="description">'.$field['desc'].'</span>
            <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
        $post_id = get_the_ID();
        $team_id = get_post_meta( $post_id, 'report_home-select');

        $items = get_users( array (
                'meta_key' => 'team-meta',
                'meta_value' => $team_id
            ));
        $i = 0;

        if ($meta) {
            foreach($meta as $row) {

                echo '<li><span class="sort hndle button">|||</span>';
                echo '<select name="'.$field['id'].'['.$i.']" style="width:200px;" id="report_home-scorers">
                <option value="">Select One</option>'; // Select One
                foreach($items as $item) {
                    echo '<option value="'.$item->ID.'"',$row == $item->ID ? ' selected="selected"' : '','>'.$item->display_name.'</option>';
                }
                echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

                $i++;
            }
        } else {
            echo '<li><span class="sort hndle button">|||</span>';
            echo '<select name="report_home-scorers[0]" style="width:200px;" id="'.$field['id'].'">
            <option value="NULL">Select One</option>'; // Select One

            foreach ($items as $item) {
                echo '<option value="" selected="selected">'.$item->display_name.'</option>';
            }

            echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

        }
        echo '</ul>';
        break;

        // away_player_list
    case 'away_player_list':
        echo '<a class="repeatable-add button" href="#">+</a> <span class="description">'.$field['desc'].'</span>
            <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
        $postid = get_the_ID();
        $team_id = get_post_meta( $postid, 'report_away-select');

        $items = get_users( array (
                'meta_key' => 'team-meta',
                'meta_value' => $team_id
            ));


        $i = 0;
        if ($meta) {
            foreach($meta as $row) {
                echo '<li><span class="sort hndle button">|||</span>';
                echo '<select name="'.$field['id'].'['.$i.']" class="chzn-select" style="width:200px;" id="'.$field['id'].'" value="'.$row.'">
                <option value="">Select One</option>'; // Select One
                foreach($items as $item) {
                    echo '<option value="'.$item->ID.'"',$row == $item->ID ? ' selected="selected"' : '','>'.$item->display_name.'</option>';
                }
                echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

                $i++;
            }
        } else {
            echo '<li><span class="sort hndle button">|||</span>';
            echo '<select name="'.$field['id'].'['.$i.']" class="chzn-select" style="width:200px;" id="'.$field['id'].'">
            <option value="">Select One</option>'; // Select One

            foreach ($items as $item) {
                echo '<option value="" selected="selected">'.$item->display_name.'</option>';
            }

            echo '</select><a class="repeatable-remove button" href="#">-</a></li>';

        }
        echo '</ul>';
        break;

    } //end switch
    echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
share|improve this question
    
What's the value of $goal_info_meta_fields? –  TheDeadMedic Jun 24 '13 at 9:57
    
I added it below. –  mantis Jun 24 '13 at 10:18
    
Can you also post the code you're using to output the nonce field, i.e. the wp_nonce_field() bit. –  TheDeadMedic Jun 24 '13 at 10:29
    
There you are. It's a bit long... –  mantis Jun 24 '13 at 10:52

1 Answer 1

up vote 0 down vote accepted

The issue was that you should have changed product_noncename to goal_info_meta_box_nonce. I've also made a few other changes, commenting out the old lines for comparison, which should avoid any further errors (including the undefined index).

// verify nonce
// if ( ! isset( $_POST['product_noncename'] ) || ! wp_verify_nonce( $_POST['goal_info_meta_box_nonce'], basename( __file__ ) ) ) {
if ( ! isset( $_POST['goal_info_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['goal_info_meta_box_nonce'], basename( __file__ ) ) ) {
    return $post_id;
}

...

// loop through fields and save the data
foreach ( $goal_info_meta_fields as $field ) {
    // if ( $field['type'] == 'tax_select' )
    if ( $field['type'] == 'tax_select' || ! isset( $_POST[ $field['id'] ] ) )
        continue;

    $old = get_post_meta( $post_id, $field['id'], true );
    $new = $_POST[ $field['id'] ];

    // if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
    if ( ! empty( $new ) && $new != $old ) {
        update_post_meta( $post_id, $field['id'], $new );
    } elseif ( '' == $new && $old ) {
        delete_post_meta( $post_id, $field['id'], $old );
    }
}
share|improve this answer
    
Oh how awful! I can't believe I didn't catch that! Thank you for pointing it out. You've saved me another few hours of tearing my hair out. I copied and pasted from another response to a similar question. –  mantis Jun 24 '13 at 11:19

Your Answer

 
discard

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.