0

I'm trying to save a metabox input but it doesn't seem to work. I'm working with arrays (as I need my metabox to have 60 rows), so I assume the problem lies within them.

This is my metabox function for the admin (which works correctly showing the info I wanna show):

function mock_metabox() {
    global $post;
    // Nonce field
    wp_nonce_field( basename( __FILE__ ), 'mock_fields' );
    // init counter for meta array
    $contadorglobal = 1;
    $selecciones = array();
    $equipos = array();
    $equiposog = array();
    while ( $contadorglobal <= 60 ){
        $selecciones[$contadorglobal-1] = get_post_meta( $post->ID, '_seleccion_' . $contadorglobal, true );
        $equipos[$contadorglobal-1] = get_post_meta( $post->ID, '_equipo_' . $contadorglobal, true );
        $equiposog[$contadorglobal-1] = get_post_meta( $post->ID, '_equipoog_' . $contadorglobal, true );
        $contadorglobal++;
    }

    // Output the fields
    ?>
    <h3> Informacion del Mock </h3>
    <table>
    <tr>
    <th> # </th>
    <th> Jugador </th>
    <th> Equipo </th>
    <th> Equipo Original </th>
    </tr>

    <?

    $contador = 1;

    $teams = get_posts( array(
            'post_type' => 'team',
            'orderby'   => 'title',
            'order'     => 'ASC',
            'numberposts' => -1,
            'post_status' => 'publish'
        ) );

    while ( $contador <= 60 ){
        ?>
        <tr>
            <td><?php echo $contador ?></td>
            <td><input type="text" name="<? 'jugador_' . $contador ?>" value="<?php echo $selecciones[$contador-1] ; ?>" />
            <td><select name="<? 'equipo_' . $contador?>" ><?
            foreach ( $teams as $team ) { ?>
                <option value="<?php echo $team->ID; ?>" <?php checked( $equipos[$contador-1], $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
            <td><select name="<? 'equipoog_' . $contador ?>" ><?
            foreach ( $teams as $team ) { ?>
                <option value="<?php echo $team->ID; ?>" <?php checked( $equiposog[$contador-1], $team->ID ); ?>  > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
            <? $contador++; ?>
        </tr>
        <?php } ?>
    </table>


<?}

And this is the saving function (I thought the problem was with the while, but I tried removing it and it doesn't save any info either).

function mock_save_meta_box_data( $post_id ){
    // verify taxonomies meta box nonce
    if ( !isset( $_POST['mock_fields'] ) || !wp_verify_nonce( $_POST['mock_fields'], basename( __FILE__ ) ) ){
        return;
    }
    // return if autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
        return;
    }
    // Check the user's permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ){
        return;
    }
    // guarda tipo traspaso
    $contadorid = 1;
    if ( isset( $_REQUEST['jugador_' . $contadorid] ) ) {
        update_post_meta( $post_id, '_seleccion_' . $contadorid, $_POST['jugador_' . $contadorid] );
        }
    if ( isset( $_REQUEST['equipo_' . $contadorid] ) ) {
        update_post_meta( $post_id, '_equipo_' . $contadorid, $_POST['equipo_' . $contadorid] );
        }
    if ( isset( $_REQUEST['equipoog_' . $contadorid] ) ) {
        update_post_meta( $post_id, '_equipoog_' . $contadorid, $_POST['equipoog_' . $contadorid] );
        }
    }

add_action( 'save_post_mock', 'mock_save_meta_box_data' );

Any ideas? Thanks in advance!

0

The main problem is you're not echoing names of your fields. Like <input type="text" name="<? 'jugador_' . $contador ?>" should be <input type="text" name="<? echo 'jugador_' . $contador ?>"

Still, I have a better suggestion. You can use arrays in your form submission. So your metabox callback function should look like this:

function mock_metabox() {
    global $post;
    // Nonce field
    wp_nonce_field( basename( __FILE__ ), 'mock_fields' );
    // init counter for meta array
    $selecciones = get_post_meta( $post->ID, '_seleccion', true );
    $equipos = get_post_meta( $post->ID, '_equipo', true );
    $equiposog = get_post_meta( $post->ID, '_equipoog', true );

    // Output the fields
    ?>
    <h3> Informacion del Mock </h3>
    <table>
    <tr>
    <th> # </th>
    <th> Jugador </th>
    <th> Equipo </th>
    <th> Equipo Original </th>
    </tr>

    <?

    $contador = 1;

    $teams = get_posts( array(
            'post_type' => 'team',
            'orderby'   => 'title',
            'order'  => 'ASC',
            'numberposts' => -1,
            'post_status' => 'publish'
        ) );

    while ( $contador <= 60 ){
        $v_jugador = isset( $selecciones[$contador-1] ) ? $selecciones[$contador-1] : '';
        $v_equipo = isset( $equipos[$contador-1] ) ? $equipos[$contador-1] : '';
        $v_equiposog = isset( $equiposog[$contador-1] ) ? $equiposog[$contador-1] : '';
        ?>
        <tr>
            <td><?php echo $contador ?></td>
            <td><input type="text" name="jugador[]" value="<?php echo $v_jugador; ?>" />
            <td><select name="equipo[]"><?
            foreach ( $teams as $team ) { ?>
                <option value="<?php echo $team->ID; ?>" <?php selected( $v_equipo, $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
            <td><select name="equipoog[]"><?
            foreach ( $teams as $team ) { ?>
                <option value="<?php echo $team->ID; ?>" <?php selected( $v_equiposog, $team->ID ); ?>  > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
            <? $contador++; ?>
        </tr>
        <?php } ?>
    </table>
    <?
}

And the updating function to this:

function mock_save_meta_box_data( $post_id ){
    // verify taxonomies meta box nonce
    if ( !isset( $_POST['mock_fields'] ) || !wp_verify_nonce( $_POST['mock_fields'], basename( __FILE__ ) ) ){
        return;
    }
    // return if autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
        return;
    }
    // Check the user's permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ){
        return;
    }
    // guarda tipo traspaso
    if ( isset( $_POST['jugador'] ) ) {
        update_post_meta( $post_id, '_seleccion', $_POST['jugador'] );
    }
    if ( isset( $_POST['jugador'] ) ) {
        update_post_meta( $post_id, '_equipo', $_POST['equipo'] );
    }
    if ( isset( $_POST['jugador'] ) ) {
        update_post_meta( $post_id, '_equipoog', $_POST['equipoog'] );
}

add_action( 'save_post_mock', 'mock_save_meta_box_data' );

Give it a try.

Edit: Don't use checked() in select field's options. Use selected() function.

  • That works! Thanks. If I wanna recover the meta info now, I just handle it like an array I guess right? – DRZN93 May 22 '18 at 7:57
  • Yes, just like other array. You can check output with print_r(). Also please select as correct answer if it worked :) – Sohan Zaman May 22 '18 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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