创建并保存多个元框

时间:2012-09-04 作者:MF1

我创建了一个自定义帖子类型和多个需要保存的元数据库。我有一个保存一个元盒的函数,但我不知道如何重用此函数来保存所有元盒。

add_action ("add_meta_boxes", "add_dedications_jh");

function add_dedications_jh() {
     add_meta_box(\'jh_dedicationDate\', \'Enter Date\', \'jh_dedicationDate\', \'jh_dedications\', \'side\', \'default\');
     add_meta_box(\'jh_dedicationName\', \'Enter Name\', \'jh_dedicationName\', \'jh_dedications\', \'normal\', \'default\');
     add_meta_box(\'jh_dedicationOccasion\', \'Enter Occasion\', \'jh_dedicationOccasion\', \'jh_dedications\', \'normal\', \'default\');
}

function jh_dedicationDate($object, $box) {
    wp_nonce_field( basename( __FILE__ ), \'dedicationDate_nonce\' ); 
    ?>
    <input type="text" name="dedicationDate" value="<?php echo esc_attr( get_post_meta( $object->ID, \'dedicationDate\', true) ); ?>" />
    <?php 
}

function jh_dedicationName ($object, $box) {
    wp_nonce_field( basename( __FILE__ ), \'dedicationName_nonce\' ); 
    ?>
    <input type="text" size="60" name="dedicationName" value="<?php echo esc_attr( get_post_meta( $object->ID, \'dedicationName\', true) ); ?>" /> 
    <?php
}

function jh_dedicationOccasion ($object, $box) {
    wp_nonce_field( basename( __FILE__ ), \'dedicationOccasion_nonce\' ); 
    ?>
    <input type="text" size="60" name="jh_dedicationOccasion" value="<?php echo esc_attr( get_post_meta( $object->ID, \'dedicationOccasion\', true) ); ?>" /> 
    <?php
}

add_action(\'save_post\', \'jh_save_dedication\', 10, 2);

function jh_save_dedication($post_id, $post) {
    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST["dedicationDate_nonce"] ) || !wp_verify_nonce( $_POST["dedicationDate_nonce"], basename( __FILE__ ) ) )
        return $post_id;
    if ( !isset( $_POST["dedicationName_nonce"] ) || !wp_verify_nonce( $_POST["dedicationName_nonce"], basename( __FILE__ ) ) )
        return $post_id;
    if ( !isset( $_POST["dedicationOccasion_nonce"] ) || !wp_verify_nonce( $_POST["dedicationOccasion_nonce"], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );
    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST[\'dedicationDate\'] ) ? sanitize_html_class( $_POST[\'dedicationDate\'] ) : \'\' );
    /* Get the meta key. */
    $meta_key = \'dedicationDate\';
    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );
    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );
    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
}

1 个回复
最合适的回答,由SO网友:Upeksha 整理而成

您必须为其他2个nonce字段重复代码的最后9行。考虑以下功能

function wp64123_sanitize_save_meta($nonce_field){

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST[$nonce_field] ) ? sanitize_html_class( $_POST[$nonce_field] ) : \'\' );

    /* Get the meta key. */
    $meta_key = $nonce_field;

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

        /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );
        /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );
        /* If there is no new meta value but an old value exists, delete it. */
    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );      
}
现在必须为每个nonce字段调用上述函数。

wp64123_sanitize_save_meta("dedicationDate");
wp64123_sanitize_save_meta("dedicationName");
wp64123_sanitize_save_meta("dedicationOccassion");
如果您在WP管理区域内提交和处理表单,那么您可以使用check_admin_referer() 而不是wp_verify_nonce()

if ( !empty($_POST) && check_admin_referer(\'name_of_my_action\',\'name_of_nonce_field\') )
{
    // process form data
}   
阅读更多Codexhttp://codex.wordpress.org/Function_Reference/wp_nonce_field

结束