$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Meta Box clears saved field content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Meta Box clears saved field content

matteradmin10PV0评论

I'm new in WP developing. I added metabox field to page, all works fine, but last one, which is similar with others, when I press save page button it clears all fields, even fields which were saved before. What I'm doing wrong? Or is it limitation in using metabox?

here is my code

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;
}
}

I'm new in WP developing. I added metabox field to page, all works fine, but last one, which is similar with others, when I press save page button it clears all fields, even fields which were saved before. What I'm doing wrong? Or is it limitation in using metabox?

here is my code

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;
}
}
Share Improve this question edited Oct 24, 2018 at 16:37 Marat asked Oct 24, 2018 at 16:29 MaratMarat 14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found it, it was actually clearing when inputting apostrophe (') which conflicts in database save, to solve I used htmlentities() adn js replace to replace and save apostrophe with apostrophe code.

<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');
} 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far