WordPress元表格数据不保存

时间:2017-06-01 20:56:39

标签: php wordpress

我的插件是将常见问题解答添加到您想要的页面。

  1. 我在“添加新问题”中的元数据框中添加了一个表单。
  2. 当我在表单中提供值时,表单数据会保存 数据库,但不保留或显示值(选中) 在页面更新/发布时的形式。

  3. 相反,表单显示以前未选择的值(即使是 选定的值确实显示在数据库中。

  4. <?php
    
    function faq_manage_things($post)
    {
    
    global $post;
    $post_id= $post->ID;
    wp_nonce_field(basename(__FILE__),'faqs_questions_nonce');
    $faq_stored_meta= get_post_meta($post->ID);
    
    ?>
        <div class="wrap">
        <p>
             <label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label>
                <select name="meta-select" id="meta-select">
                    <option value="select-one" <?php if ( isset ( $prfx_stored_meta['meta-select'] ) ) selected( $prfx_stored_meta['meta-select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>';
                    <option value="select-two" <?php if ( isset ( $prfx_stored_meta['meta-select'] ) ) selected( $prfx_stored_meta['meta-select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>';
                </select>
        </p>
    
        <!-- TEXT AREA -->
    
        <p>
            <label for="meta-textarea" class="prfx-row-title"><?php _e( 'Example Textarea Input', 'prfx-textdomain' )?></label>
            <textarea name="meta-textarea" id="meta-textarea"><?php if ( isset ( $prfx_stored_meta['meta-textarea'] ) ) echo $prfx_stored_meta['meta-textarea'][0]; ?></textarea>
        </p>
    
    
            <!-- CHECKBOXES -->
    
        <p>
            <span class="prfx-row-title"><?php _e( 'Example Checkbox Input', 'prfx-textdomain' )?></span>
                <div class="prfx-row-content">
                    <label for="meta-checkbox">
                        <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox'] ) ) checked( $prfx_stored_meta['meta-checkbox'][0], 'yes' ); ?> /><?php _e( 'Checkbox label', 'prfx-textdomain' )?>
                    </label>
                    <label for="meta-checkbox-two">
                        <input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox-two'] ) ) checked( $prfx_stored_meta['meta-checkbox-two'][0], 'yes' ); ?> /><?php _e( 'Another checkbox', 'prfx-textdomain' )?>
                    </label>
                </div>
        </p>
    
    
        </div>
    <?php   
    }
    
    global $post;
    function faq_meta_save( $post_id ) {
        global $post;
        $post_id= $post->ID;
        $is_autosave = wp_is_post_autosave( $post_id ); 
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ 'faqs_questions_nonce' ] ) && wp_verify_nonce( $_POST['faqs_questions_nonce'],basename(__FILE__)) )? 'true' : 'false';
    
    
        if ( $is_autosave || $is_revision || !$is_valid_nonce ) 
            {return;} //exit
    
        if( isset( $_POST[ 'meta-select' ] ) ) 
            {   update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] );   }
    
        if( isset( $_POST[ 'meta-textarea' ] ) ) { 
            update_post_meta( $post_id, 'meta-textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) );   }
    
        // Checks for input and saves
        if( isset( $_POST[ 'meta-checkbox' ] ) ) {
            update_post_meta( $post_id, 'meta-checkbox', 'yes' );} 
        else {
            update_post_meta( $post_id, 'meta-checkbox', '' );  }
    
        // Checks for input and saves
        if( isset( $_POST[ 'meta-checkbox-two' ] ) ) {
            update_post_meta( $post_id, 'meta-checkbox-two', 'yes' );}
        else {
            update_post_meta( $post_id, 'meta-checkbox-two', '' );  }
    
        }
    
        add_action('save_post','faq_meta_save');
    

1 个答案:

答案 0 :(得分:0)

您必须检查数据库中是否有同一个post_id重复的meta_key。另外,我建议您在meta_key名称中使用下划线而不是连字符。尝试使用此代码删除post_id的meta_data,然后添加新值。

试试这个:

<?php

function faq_manage_things($post)
{

    global $post;
    $post_id= $post->ID;
    wp_nonce_field(basename(__FILE__),'faqs_questions_nonce');
    $faq_stored_meta= get_post_meta($post->ID);

    ?>
    <div class="wrap">
        <p>
            <label for="meta-select" class="prfx-row-title"><?php _e( 'Example Select Input', 'prfx-textdomain' )?></label>
            <select name="meta-select" id="meta-select">
                <option value="select-one" <?php if ( isset ( $prfx_stored_meta['meta_select'] ) ) selected( $prfx_stored_meta['meta_select'][0], 'select-one' ); ?>><?php _e( 'One', 'prfx-textdomain' )?></option>';
                <option value="select-two" <?php if ( isset ( $prfx_stored_meta['meta_select'] ) ) selected( $prfx_stored_meta['meta_select'][0], 'select-two' ); ?>><?php _e( 'Two', 'prfx-textdomain' )?></option>';
            </select>
        </p>

        <!-- TEXT AREA -->

        <p>
            <label for="meta-textarea" class="prfx-row-title"><?php _e( 'Example Textarea Input', 'prfx-textdomain' )?></label>
            <textarea name="meta-textarea" id="meta-textarea"><?php if ( isset ( $prfx_stored_meta['meta_textarea'] ) ) echo $prfx_stored_meta['meta_textarea'][0]; ?></textarea>
        </p>


        <!-- CHECKBOXES -->

        <p>
            <span class="prfx-row-title"><?php _e( 'Example Checkbox Input', 'prfx-textdomain' )?></span>
            <div class="prfx-row-content">
                <label for="meta-checkbox">
                    <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['meta_checkbox'] ) ) checked( $prfx_stored_meta['meta_checkbox'][0], 'yes' ); ?> /><?php _e( 'Checkbox label', 'prfx-textdomain' )?>
                </label>
                <label for="meta-checkbox-two">
                    <input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if ( isset ( $prfx_stored_meta['meta_checkbox_two'] ) ) checked( $prfx_stored_meta['meta_checkbox_two'][0], 'yes' ); ?> /><?php _e( 'Another checkbox', 'prfx-textdomain' )?>
                </label>
            </div>
        </p>


    </div>
    <?php   
}

global $post;


function faq_meta_save( $post_id ) {
    global $post;
    $post_id= $post->ID;
    $is_autosave = wp_is_post_autosave( $post_id ); 
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'faqs_questions_nonce' ] ) && wp_verify_nonce( $_POST['faqs_questions_nonce'],basename(__FILE__)) )? 'true' : 'false';


    if ( $is_autosave || $is_revision || !$is_valid_nonce ) 
        {return;} //exit

    if( isset( $_POST[ 'meta-select' ] ) ) {   
        remove_post_meta( $post_id, 'meta_select', $_POST[ 'meta-select' ] );
        add_post_meta( $post_id, 'meta_select', $_POST[ 'meta-select' ] );   
    }

    if( isset( $_POST[ 'meta-textarea' ] ) ) { 
        remove_post_meta( $post_id, 'meta_textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) );
        add_post_meta( $post_id, 'meta_textarea', sanitize_text_field($_POST[ 'meta-textarea' ]) );   
    }

    // Checks for input and saves
    if( isset( $_POST[ 'meta-checkbox' ] ) ) {
        remove_post_meta( $post_id, 'meta_checkbox', 'yes' ); 
        add_post_meta( $post_id, 'meta_checkbox', 'yes' );
    } 
    else {
        remove_post_meta( $post_id, 'meta_checkbox');  
    }

    // Checks for input and saves
    if( isset( $_POST[ 'meta-checkbox-two' ] ) ) {
        remove_post_meta( $post_id, 'meta_checkbox-two', 'yes' );
        add_post_meta( $post_id, 'meta_checkbox-two', 'yes' );
    }
    else {
        remove_post_meta( $post_id, 'meta_checkbox-two');  
    }
}
add_action('save_post','faq_meta_save');
相关问题