在保存元数据时遇到问题

时间:2018-08-07 作者:klewis

我所做的就是为我的帖子创建一个简单的元框,调用现有的标记,然后将它们显示在“选择”下拉列表中,供编辑器选择。我觉得我离完成这件事太近了。。。但是当我保存帖子时,我选择的元值没有保存。为了完全保存我的数据,我是否需要重新考虑或调整某个步骤?

//meta box functions for adding the meta box and saving the data
add_action(\'add_meta_boxes\',\'nffa_meta_box_tags\');

function nffa_meta_box_tags() {    
add_meta_box(\'nffa_meta\', \'Project Tag Status\',\'nffa_meta_box\',\'post\',\'side\',\'high\');
}

function nffa_meta_box($post) {

    //GET TAGS
    $args = array(
        \'orderby\'    => \'name\',
        \'order\'      => \'ASC\',
        \'hide_empty\' => 0,
        \'taxonomy\'   => \'post_tag\'
        );

    $categories=get_categories($args);
        foreach($categories as $category) { 
          $tags[] =  $category->name ;  
        }

    $prefix = \'NFFA_meta_\';
    $mtags = array(
        \'name\' => \'tags\',
        \'desc\' => \'Project Status:\',
        \'subdesc\' => \'Please set the status of this project\',
        \'id\' => $prefix . \'tags\',
        \'type\' => \'select\',
        \'options\' => $tags
    );

    print "<pre>";
    print_r($mtags);
    print "</pre>";

    //retrieve the custom meta box values
    $nffa_meta_value = get_post_meta($post->ID,$mtags[\'id\'], true);
    //nonce for security
    wp_nonce_field(plugin_basename(__FILE__),\'nffa_save_meta_box\');
    //custom meta box form elements via select
    echo \'<p>\'.$mtags[\'desc\'].\' <select name="\'. $mtags[\'id\'].\'" id="\'.$mtags[\'id\'].\'">\';
    foreach ($mtags[\'options\'] as $vtag) {
        echo \'<option\', $nffa_meta_value == $vtag ? \' selected="selected"\' : \'\', \'>\', $vtag, \'</option>\';
    }
    echo \'</select>\';
    echo \'</p>\';
    echo \'<p class="howto" id="new-tag-post_tag-desc">\'.$mtags[\'subdesc\'].\'</p>\';
}


//save our meta data
add_action (\'save_post\',\'nffa_save_meta_box_tags\');
function nffa_save_meta_box_tags($post_id) {
    //process form data if $_POST is set
    if (isset($_POST[$mtags[\'id\']])) {
        //if auto saving skip saving our meta box data
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
        return;

        //check nonce for security
        check_admin_referer(
            plugin_basename(__FILE__),\'nffa_save_meta_box\');
            //save the metabox data as post meta using the post ID as a  unqiue prefix
            update_post_meta($post_id, $mtags[\'id\'], santize_text_field($_POST[$mtags[\'id\']]));
    }
}

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

在里面nffa_save_meta_box_tags(), 您使用$mtags, 但是$mtags 未在该函数中定义。手动替换的两个实例$mtags[\'id\'] 具有\'NFFA_meta_tags\' 保存值-显示这是问题的一部分。

另一个问题是santize_text_field() 有拼写错误,应该是sanitize_text_field().

结束

相关推荐

如何在编辑或添加帖子屏幕上添加Metabox来拉取自定义帖子列表(任意两个)?

我想将metabox添加到自定义帖子列表中,并在编辑或添加帖子屏幕上选择任意两个。最后,当文章发布时,将其显示在单个文章页面上。请帮帮我!任何帮助都将不胜感激。。。。 add_action( \'add_meta_boxes\', function () { add_meta_box( \'yourcustom_sectionid\', __( \' Custom Offer Section\', \'yourtextdomain\'