0

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

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.