最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - saving dropdown menu data on custom post type

programmeradmin2浏览0评论

I'm following this tutorial on how to make a custom field. I already used it before and it worked, but I only did it for text inputs. Now I'd like to make a drop down menu and I'm unable. The thing shows up as expected but it is just not saving... Can you spot what I'm missing?

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'sobreOProjeto', 'Sobre o projeto', 'projeto_callback', 'Projetos', 'normal', 'default' );
}

function projeto_callback($post) {
    global $post;
    $values = get_post_custom( $post->ID );
    $text = isset( $values['ano'] ) ? esc_attr( $values['ano'][0] ) : '';
    $selected = isset( $values['estadoDaObra'] ) ? esc_attr( $values['estadoDaObra'][0] ) : '';
    $check = isset( $values['institucional'] ) ? esc_attr( $values['institucional'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

    ?>
    <p>
        <label for="ano">Ano de Criação</label>
        <input type="text" name="ano" id="ano" value="<?php echo $text; ?>" />
    </p>
    <p>
        <label for="estadoDaObra">Estado da Obra</label>
        <select name="estadoDaObra" id="estadoDaObra">
            <option value="não construído" <?php selected( $selected, 'nao_construido' ); ?>>não construído</option>
            <option value="em construção" <?php selected( $selected, 'em_construcao' ); ?>>em construção</option>
            <option value="construído" <?php selected( $selected, 'construido' ); ?>>construído</option>
        </select>
    </p>

    <p>
        <input type="checkbox" id="institucional" name="institucional" <?php checked( $check, 'off' ); ?> />
        <label for="institucional">Institucional</label>
    </p>
    <?php
}

    add_action( 'save_post', 'sobreAObra_salvar' );
    function sobreAObra_salvar( $post_id ){
        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return;

        // now we can actually save the data
        $allowed = array( 
            'a' => array( // on allow a tags
                'href' => array() // and those anchors can only have href attribute
            )
        );

        // Make sure your data is set before trying to save it
        if( isset( $_POST['ano'] ) )
            update_post_meta( $post_id, 'ano', wp_kses( $_POST['ano'], $allowed ) );

        if( isset( $_POST['estadoDaObra'] ) )
            update_post_meta( $post_id, 'estadoDaObra', esc_attr( $_POST['estadoDaObra'] ) );

        // This is purely my personal preference for saving check-boxes
        $chk = isset( $_POST['institucional'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'institucional', $chk );
    }

I'm following this tutorial on how to make a custom field. I already used it before and it worked, but I only did it for text inputs. Now I'd like to make a drop down menu and I'm unable. The thing shows up as expected but it is just not saving... Can you spot what I'm missing?

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'sobreOProjeto', 'Sobre o projeto', 'projeto_callback', 'Projetos', 'normal', 'default' );
}

function projeto_callback($post) {
    global $post;
    $values = get_post_custom( $post->ID );
    $text = isset( $values['ano'] ) ? esc_attr( $values['ano'][0] ) : '';
    $selected = isset( $values['estadoDaObra'] ) ? esc_attr( $values['estadoDaObra'][0] ) : '';
    $check = isset( $values['institucional'] ) ? esc_attr( $values['institucional'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

    ?>
    <p>
        <label for="ano">Ano de Criação</label>
        <input type="text" name="ano" id="ano" value="<?php echo $text; ?>" />
    </p>
    <p>
        <label for="estadoDaObra">Estado da Obra</label>
        <select name="estadoDaObra" id="estadoDaObra">
            <option value="não construído" <?php selected( $selected, 'nao_construido' ); ?>>não construído</option>
            <option value="em construção" <?php selected( $selected, 'em_construcao' ); ?>>em construção</option>
            <option value="construído" <?php selected( $selected, 'construido' ); ?>>construído</option>
        </select>
    </p>

    <p>
        <input type="checkbox" id="institucional" name="institucional" <?php checked( $check, 'off' ); ?> />
        <label for="institucional">Institucional</label>
    </p>
    <?php
}

    add_action( 'save_post', 'sobreAObra_salvar' );
    function sobreAObra_salvar( $post_id ){
        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return;

        // now we can actually save the data
        $allowed = array( 
            'a' => array( // on allow a tags
                'href' => array() // and those anchors can only have href attribute
            )
        );

        // Make sure your data is set before trying to save it
        if( isset( $_POST['ano'] ) )
            update_post_meta( $post_id, 'ano', wp_kses( $_POST['ano'], $allowed ) );

        if( isset( $_POST['estadoDaObra'] ) )
            update_post_meta( $post_id, 'estadoDaObra', esc_attr( $_POST['estadoDaObra'] ) );

        // This is purely my personal preference for saving check-boxes
        $chk = isset( $_POST['institucional'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'institucional', $chk );
    }
Share Improve this question edited Dec 3, 2014 at 14:15 GuiHarrison asked Dec 3, 2014 at 5:13 GuiHarrisonGuiHarrison 1151 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I've found my problem. The choice was indeed being saved, just wasn't showing to me at the editor (it was always showing me the first choice). The thing is, the $selected function compares the selected input with the string provided which, in my case, was incorrectly filled. so the following code:

    <select name="estadoDaObra" id="estadoDaObra">
        <option value="não construído" <?php selected( $selected, 'nao_construido' ); ?>>não construído</option>
        <option value="em construção" <?php selected( $selected, 'em_construcao' ); ?>>em construção</option>
        <option value="construído" <?php selected( $selected, 'construido' ); ?>>construído</option>
    </select>

should actually be:

    <select name="estadoDaObra" id="estadoDaObra">
        <option value="não construído" <?php selected( $selected, 'não construído' ); ?>>não construído</option>
        <option value="em construção" <?php selected( $selected, 'em construção' ); ?>>em construção</option>
        <option value="construído" <?php selected( $selected, 'construído' ); ?>>construído</option>
    </select>

Dumb thing to get wrong and would be hard for me get any answers here, since I was too lazy or too tired to translate those values to English.

Anyway, if you are strugling with that, just know that you just have to have the same text on both places.

Thanks to anyone who read this, even if you didn't manage to figure it out.

发布评论

评论列表(0)

  1. 暂无评论