Meta Box清除保存的字段内容

时间:2018-10-24 作者:Marat

我是WP开发的新手。我在页面中添加了metabox字段,一切正常,但最后一个类似于其他字段,当我按下保存页面按钮时,它会清除所有字段,甚至是以前保存的字段。我做错了什么?还是使用metabox有局限性?

这是我的密码

add_action(\'add_meta_boxes\', \'add_denet_faq_meta\');

function add_denet_faq_meta(){
global $post;

if(!empty($post))
{
    $pageTemplate = get_post_meta($post->ID, \'_wp_page_template\', true);
    if($pageTemplate == \'faq.php\' )
    {
        remove_post_type_support( \'page\', \'editor\' );
        add_meta_box(
            \'economic_meta\',
            \'Economic and Tokenomic\',
            \'faq_economic\',
            \'page\',
            \'normal\',
            \'high\');
    }
  }
}

function faq_economic(){
global $post;

wp_nonce_field( basename( __FILE__ ), \'page_meta_box_nonce\' );
$faq_3_title = get_post_meta($post->ID, "_denet_faq_3", true);

$no_faq_3 = get_post_meta($post->ID, "_denet_no_faq_3", true);

$faq_3_question = get_post_meta($post->ID, "_faq_3_question", true);
$faq_3_question = ($faq_3_question != \'\') ? json_decode($faq_3_question) : array();

$faq_3_answer = get_post_meta($post->ID, "_faq_3_answer", true);
$faq_3_answer = ($faq_3_answer != \'\') ? json_decode($faq_3_answer) : array();
$counterX3 = 0;

echo \'<input type="hidden" class="slide-fields x" id="val3" name="no_faq_3" type="number" value="\'. $no_faq_3 .\'" placeholder="0">\';
echo \'<h2 style="padding:0;"><input type="text" name="faq_3_title" style="width:100%;" placeholder="Tab Title" value="\'. $faq_3_title . \'"></input></h2>
<hr/>\';

for ($ic = 0; $ic < $no_faq_3; $ic++ ) {
    if ($faq_3_question != " ") {
        echo \'<div class="row">
<div class="col-md-4 col-sm-12"><input name="faq_3_question[]" style="width: 100%;" placeholder="Question" value="\'. $faq_3_question[$counterX3] .\'"></input></div>
<div class="col-md-7 col-sm-10" style="padding: 0;"><textarea name="faq_3_answer[]" style="width: 100%;" placeholder="Answer">\'. $faq_3_answer[$counterX3++] .\'</textarea></div>
<div class="col-md-1 col-sm-2"><span style="margin-top: 5px;" class="dashicons dashicons-no"></span></div>
</div>
<hr/>\';
}}
echo \'<input id="add3" name="add3" type="submit" class="btn sbmt tb" value="ADD MORE +"></input>\';
}

add_action(\'save_post_page\', \'denet_faq_meta_box_save\');
function denet_faq_meta_box_save($post_id) {
if ( !isset( $_POST[\'page_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'page_meta_box_nonce\'], basename( __FILE__ ) ) ){
    return;
}
// return if autosave
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ){
    return;
}
// check the user\'s permissions.
if ( ! current_user_can( \'edit_page\', $post_id ) ){
    return;
}

if ( isset( $_REQUEST[\'no_faq_3\'] ) ) {
    update_post_meta( $post_id, \'_denet_no_faq_3\', sanitize_text_field( $_POST[\'no_faq_3\'] ) );
}

if ( isset( $_REQUEST[\'faq_3_question\'] ) ) {
    $faq_3_question = (isset($_POST[\'faq_3_question\']) ? $_POST[\'faq_3_question\'] : \'\');
    $faq_3_question = strip_tags(json_encode($faq_3_question));
    update_post_meta($post_id, "_faq_3_question", $faq_3_question);

    $faq_3_answer = (isset($_POST[\'faq_3_answer\']) ? $_POST[\'faq_3_answer\'] : \'\');
    $faq_3_answer = strip_tags(json_encode($faq_3_answer));
    update_post_meta($post_id, "_faq_3_answer", $faq_3_answer); 
}

else {
    return $post_id;
}
}

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

我发现了,实际上是在输入撇号(\')时清除了数据库保存中的冲突,为了解决这个问题,我使用了htmlentities()adn js replace来替换撇号,并用撇号代码保存撇号。

<input onkeyup="valid(this)" type="text" name="faq_2_title" style="width:100%;" placeholder="Tab Title" value="\'. htmlentities($faq_2_title) . \'"></input></h2>
js公司:

function valid(f) {
  f.value = f.value.replace(/\'/g, \'&#39\');
} 

结束