中继器输入字段数据未正确保存-Wordpress

时间:2018-07-10 07:16:21

标签: php jquery wordpress

我正在尝试保存在自定义输入框中输入的数据,单击该按钮后输入框将重复出现。当我更新页面时,内容未更新。仅第一个字段或第一个输入正在更新,但并非所有字段都在更新。这是我的代码,用于在管理员端创建自定义输入框。

function call_creditorsClass() {
     new creditorsClass();
}

if ( is_admin() ) {
    add_action( 'load-post.php',     'call_creditorsClass' );
    add_action( 'load-post-new.php', 'call_creditorsClass' );
}


class creditorsClass {


   public function __construct() {
    add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
    add_action( 'save_post',      array( $this, 'save'         ) );
}

/**
 * Adds the meta box container.
 */
public function add_meta_box( $post_type ) {
    // Limit meta box to certain post types.
    // $post_types = array( 'post', 'page' );
    // if ( in_array( $post_type, $post_types ) ) {

    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if ($post_id == '307'){
        add_meta_box(
            'my_all_meta',
            __( 'Creditors table', 'textdomain' ),
            array( $this, 'render_meta_box_content' ),
            $post_type,
            'advanced',
            'high'
        );
    }
}

/**
 * Save the meta when the post is saved.
 *
 * @param int $post_id The ID of the post being saved.
 */
public function save( $post_id ) {

    /*
     * We need to verify this came from the our screen and with proper authorization,
     * because save_post can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
        return $post_id;
    }

    $nonce = $_POST['myplugin_inner_custom_box_nonce'];

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
        return $post_id;
    }

    /*
     * If this is an autosave, our form has not been submitted,
     * so we don't want to do anything.
     */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // Check the user's permissions.
    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        }
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }

    /* OK, it's safe for us to save the data now. */

    // Sanitize the user input.
    $mydata = sanitize_text_field( $_POST['myplugin_creditorName'], $_POST['myplugin_creditedAmount'] );

    // Update the meta field.
    update_post_meta( $post_id, '_my_meta_value_key', $mydata );
}


/**
 * Render Meta Box content.
 *
 * @param WP_Post $post The post object.
 */
public function render_meta_box_content( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );

    // Use get_post_meta to retrieve an existing value from the database.
    $value = get_post_meta( $post->ID, '_my_meta_value_key', true );

    // Display the form, using the current value.
    <div id="custom-meta-files">
<div class="clearfix">
    <div class="float-left">
        <h6>Name of Creditor</h6>
    </div>
    <div class="float-right">
        <h6>Amount ​( ₹ million)</h6>
    </div>
</div>
<div class="repeatable">
    <div class="repeatableDiv clearfix">
        <div class="float-left">
            <input type="text" name="myplugin_creditorName" value="<?php echo esc_attr( $value ); ?>" />
        </div>
        <div class="float-right">
            <input type="text" name="myplugin_creditedAmount" value="<?php echo esc_attr( $value ); ?>" />
        </div>
        <a href="#" class="incDecBtns">-</a>
    </div>
    <a href="#" class="incBtnDiv incDiv">+</a>
</div>
</div>
}
}

<script type="text/javascript">
jQuery.noConflict();
(function( $ ) {
    $(".incDiv").on('click', function (e) {
        e.preventDefault();
        var $self = $(this);
        $self.before($self.prev('.repeatableDiv').clone().find("input:text").val("").end());
    }); 

    $('.repeatable').on('click', '.incDecBtns' ,function(e){
        e.preventDefault();
        var countdiv = $(this).closest('.repeatable').children('.repeatableDiv').length;
        if( countdiv > 1 ){
            $(this).closest('.repeatableDiv').remove();
        }
    });
})(jQuery);
</script>

0 个答案:

没有答案