使用循环输入框自定义Metabox不保存

时间:2013-08-27 16:15:27

标签: php wordpress custom-post-type meta-boxes

我正在创建一个循环播放另一个CPT的所有帖子的元数据:'产品'。元数据库列出该CPT中的所有已发布产品,将它们与输入框一起放在表格行中。每个输入框都有一个基于CPT产品ID的唯一ID。

我根据其他帖子添加了大部分代码,但是我在保存数据方面遇到了问题。我知道它与我的value =“”,我的nonce和$ meta_value有关,但我不知道下一步该去哪里。以下是我到目前为止的代码:

<?php
add_action('admin_init', 'my_theme_on_admin_init');

function my_theme_on_admin_init() {
    add_meta_box('my_metabox',
    __('Daily Inventory Levels', 'textdomain'),
    'my_metabox_render',
    'location_inventory', 'normal', 'high'
    );
}

function my_metabox_render($post) {
    // using an underscore, prevents the meta variable
    // from showing up in the custom fields section
    global $woocommerce, $post;
    $productsList = new WP_Query( 'post_type=product&post_status=publish');
?>
<div class="inside">
<table class="form-table">
    <input type="hidden" name="inventory_noncename" id="inventory_noncename" value="<?php wp_create_nonce( 'inventory-nonce' ); ?>" />
<?php 
    while ( $productsList->have_posts() ) {
    $productsList->the_post(); 
?>
<tr><td><label for="location_inventory_product-<?php the_ID(); ?>"><?php the_title(); ?></label></td><td><input id="location_inventory_product-<?php the_ID(); ?>" name="location_inventory_product-<?php the_ID(); ?>" cols="40" rows="5" value="<?php echo $meta_key; ?>" /></td></tr>
<?php }
    wp_reset_postdata();
?>
</table>
</div>
<?php
}


/*update on save*/
add_action('save_post', 'save_postdata_dynamic_inventory_metabox' );  
function save_postdata_dynamic_inventory_metabox( $post_id ) {
    // verify if this is an auto save routine.
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return;
    }

    // Check permissions
    if ( 'location_inventory' == $_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;
    }

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if (isset($_POST['inventory_noncename'])){
            if ( !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
            return;
    } else {
            return;
    }

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

    // Get the meta key.
    $meta_key = 'location_inventory_product-'.the_ID().'';

    // 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 );

} // ends function save_postdata_dynamic_reviews_metabox

1 个答案:

答案 0 :(得分:0)

一些观察结果:

1)钩子save_post有两个参数:

add_action( 'save_post', 'save_postdata_dynamic_inventory_metabox', 10, 2 );  

function save_postdata_dynamic_inventory_metabox( $post_id, $post_object ) { }

2)我不认为检查权限是必要的(至少,从未见过它)。以防万一,这是post_object使用的示例:

if ( 'location_inventory' == $post_object->post_type )

3)简短检查:

if ( !isset($_POST['inventory_noncename']) || !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
    return;

4)在函数the_ID()中使用save_postdata_dynamic_inventory_metabox是错误的。这仅在循环内可用,您已经有$post_id

5)最后,最重要的是,你处理$meta_key的方式是错误的。例如,它没有在函数my_metabox_render中定义。请查看this Answer以获取有效工作示例。

this search query中进行研究以找到更多示例。