如何在动态添加/删除自定义域(重复器域)中添加日期选择器

时间:2017-07-06 作者:Minu

我已经创建了Dynamic Add/Remove Custom Fields (Repeater Fields) 在我的自定义帖子类型中。它工作得很好。我想添加一个新的JQuery Date Picker 领域我努力创建代码,也在网上搜索,但没有找到运气。

请帮帮我。

下面是我的代码。

function project_rewards_select_options() {
    $options = array (
        \'Option 1\' => \'Option 1\',
        \'Option 2\' => \'Option 2\',
        \'Option 3\' => \'Option 3\',
    );

    return $options;
}

function project_reward_callback( $post ) {
    wp_nonce_field( \'project_reward_nonce\', \'project_reward_nonce\' );
    $reward_value = get_post_meta( get_the_ID(), \'_project_rewards\', true );
    $options = project_rewards_select_options();

?>

    <script type="text/javascript">
    jQuery(document).ready(function( $ ){
        $( \'#add-row\' ).on(\'click\', function() {
            var row = $( \'.empty-row.screen-reader-text\' ).clone(true);
            row.removeClass( \'empty-row screen-reader-text\' );
            row.insertBefore( \'#repeatable-fieldset-one tbody>tr:last\' );
            return false;
        });

        $( \'.remove-row\' ).on(\'click\', function() {
            $(this).parents(\'tr\').remove();
            return false;
        });
    });
    </script>

    <table id="repeatable-fieldset-one" width="100%">

    <thead>
        <tr>
            <th width="15%">Reward Amount</th>
            <th width="25%">Reward Title</th>
            <th width="10%">Shipping</th>       
            <th width="45%">Reward Description</th>
            <th width="5%"></th>                    
        </tr>
    </thead>

    <tbody>

<?php

    ( $reward_value );

    foreach ( $reward_value as $field ) {

?>

    <tr>
        <td><input type="text" class="widefat" name="reward[]" value="<?php if ( isset ( $field[\'reward\'] ) ) echo esc_attr( $field[\'reward\'] ); ?>" pattern="[1-9]\\d*" /></td>

        <td><input type="text" class="widefat" name="reward_title[]" value="<?php if ( isset ( $field[\'reward_title\'] ) ) echo esc_attr( $field[\'reward_title\'] ); ?>" /></td>

        <td>
            <select class="widefat" name="reward_shipping[]">
            <?php foreach ( $options as $label => $value ) : ?>
            <option value="<?php echo $value; ?>"<?php selected( $field[\'reward_shipping\'], $value ); ?>><?php echo $label; ?></option>
            <?php endforeach; ?>
            </select>
        </td>

        <td><textarea class="widefat" name="reward_description[]"><?php if ( isset ( $field[\'reward_description\'] ) ) echo esc_attr( $field[\'reward_description\'] ); ?></textarea></td>

        <td><input type="image" class="remove-row" src="<?php bloginfo(\'template_directory\'); ?>/assets/images/remove-icon.png" alt="Submit"  width="35" height="35"></td>
    </tr>

<?php } ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><input type="text" class="widefat" name="reward[]" /></td>

        <td><input type="text" class="widefat" name="reward_title[]" /></td>

        <td>
            <select class="widefat" name="reward_shipping[]">
            <?php foreach ( $options as $label => $value ) : ?>
            <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
            <?php endforeach; ?>
            </select>
        </td>   

        <td><textarea class="widefat" name="reward_description[]" ></textarea></td>

        <td><input type="image" class="remove-row" src="<?php bloginfo(\'template_directory\'); ?>/assets/images/remove-icon.png" alt="Submit"  width="35" height="35"></td>
    </tr>

    </tbody>

    </table>

    <p><a id="add-row" class="button" href="#">Add Reward</a></p>

<?php   

}

function save_project_reward( $post_id ) {

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

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'project_reward_nonce\'], \'project_reward_nonce\' ) ) {
        return;
    }

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

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {

        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }

    }
    else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    $reward_data = array();
    $options = project_rewards_select_options();

    $rewards = $_POST[\'reward\'];
    $reward_titles = $_POST[\'reward_title\'];
    $reward_shippings = $_POST[\'reward_shipping\'];
    $reward_descriptions = $_POST[\'reward_description\'];

    $count = count( $rewards );
    for ( $i = 0; $i < $count; $i++ ) {

        if ( $rewards[$i] != \'\' ) :
            $reward_data[$i][\'reward\'] = stripslashes( strip_tags( $rewards[$i] ) );

        if ( in_array( $reward_shippings[$i], $options ) )
            $reward_data[$i][\'reward_shipping\'] = stripslashes( strip_tags( $reward_shippings[$i] ) );
                else
            $reward_data[$i][\'reward_shipping\'] = \'\';
endif;

        if ( $reward_titles[$i] != \'\' ) :
            $reward_data[$i][\'reward_title\'] = stripslashes( strip_tags( $reward_titles[$i] ) );
endif;

        if ( $reward_descriptions[$i] != \'\' ) :
            $reward_data[$i][\'reward_description\'] = stripslashes( $reward_descriptions[$i] );
endif;              

    }

    update_post_meta( $post_id, \'_project_rewards\', $reward_data );

}

add_action( \'save_post\', \'save_project_reward\' ); 

2 个回复
SO网友:LWS-Mo

Wordpress已经包含了jQuery日期选择器。因此,您可以将默认WP脚本排队。然而,据我所知,WP不包括日期选择器所需的样式
因此,请尝试将WP脚本排队,例如,来自外部源的样式:

function my_datepicker_enqueue() {
            wp_enqueue_script( \'jquery-ui-datepicker\' ); // enqueue datepicker from WP
            wp_enqueue_style( \'jquery-ui-style\', \'//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css\', true);
}
add_action( \'admin_enqueue_scripts\', \'my_datepicker_enqueue\' );
创建一个新的HTML输入字段,至少包含一个类和一个名称。输入字段的HTML:(您已经定义了一些输入字段,所以跳到脚本)

<input type="text" name="_custom_datepicker[]" id="_custom_datepicker[]" class="my-custom-datepicker-field" value="<?php echo $your_saved_value; ?>" />
然后添加一个脚本来初始化新元素的日期选择器。Script:(确保您以正确的输入元素为目标,我们在这里查看定义的类,因为我们要使用多个实例。)

jQuery(document).ready(function($){
    $(\'.my-custom-datepicker-field\').datepicker({
        dateFormat: \'dd-mm-yy\', //maybe you want something like this
        showButtonPanel: true
    });
});
<小时>Update:
起初我在阅读您的代码时遇到一些问题,您的代码中也有一些错误。首先,我看到了同样奇怪的行为。我现在在一个开发站点上安装了一个testplugin,现在它正在那里工作。

1: 您设置了保存功能,这样就不能只输入日期并保存。您必须添加金额、标题或描述才能保存。我想这是你的意图?

2: 在保存功能中,您有以下代码:

if ( $reward_descriptions[$i] != \'\' ) :
    $reward_data[$i][\'reward_date\'] = stripslashes( $reward_dates[$i] );
endif;
因此,只有当描述字段不为空时,才能保存日期?!似乎这是一个输入错误,因为其他字段看起来不错。更改为:

if ( $reward_data[$i] != \'\' ) :
    $reward_data[$i][\'reward_date\'] = stripslashes( $reward_dates[$i] );
endif;
3: 在回调代码中,有以下字段作为日期选择器字段:

<td><input type="text" name="_custom_datepicker[]" id="_custom_datepicker[]" class="my-custom-datepicker-field" value="<?php echo esc_attr( $field[\'reward_date\'] ); ?>" /></td>
以及

<td><input type="text" class="my-custom-datepicker-field" name="_custom_datepicker[]" /></td>
均已命名_custom_datepicker[].但在save函数中,您试图保存名为reward_date 相反

还有id 应该是唯一的。但你添加了相同的id 到所有日期字段。您只需删除id 从这些字段中,无论如何都不需要它。

4. 如果您想动态添加字段,则需要更改datepicker JS函数。我建议将一个JS文件排队,而不是像您那样内联(多次)加载JS代码。因此,尝试用以下内容替换JS代码:

jQuery(document).ready(function($){

    // your default code
    $( \'#add-row\' ).on(\'click\', function() {
        var row = $( \'.empty-row.screen-reader-text\' ).clone(true);
        row.removeClass( \'empty-row screen-reader-text\' );
        row.insertBefore( \'#repeatable-fieldset-one tbody>tr:last\' );
        return false;
    });

    $( \'.remove-row\' ).on(\'click\', function() {
        $(this).parents(\'tr\').remove();
        return false;
    });

    //changed to be used on dynamic field which gets added and removed from DOM
    //if the element with the class .my-custom-datepicker-field gets focus, add datepicker
    $(document).on(\'focus\', \'.my-custom-datepicker-field\', function(){
        $(this).datepicker({
            dateFormat: \'dd-mm-yy\', //maybe you want something like this
            showButtonPanel: true
        });
    });

});
在我修复了这些问题并按照WordPress的要求将JS文件放入队列后,它现在可以工作了。

SO网友:Minu

@谢谢你的帮助。我已经相应地添加了代码,但日期选择器工作异常。在第一个字段中选择的日期将反映在子字段中。工作不正常。

请告诉我哪里错了。以下是代码。

function project_rewards_select_options() {
    $options = array (
        \'Option 1\' => \'Option 1\',
        \'Option 2\' => \'Option 2\',
        \'Option 3\' => \'Option 3\',
    );

    return $options;
}

function crazicle_project_reward_callback( $post ) {
    wp_nonce_field( \'project_reward_nonce\', \'project_reward_nonce\' );
    $reward_value = get_post_meta( get_the_ID(), \'_project_rewards\', true );
    $options = project_rewards_select_options();

?>

    <script type="text/javascript">
    jQuery(document).ready(function( $ ){
        $( \'#add-row\' ).on(\'click\', function() {
            var row = $( \'.empty-row.screen-reader-text\' ).clone(true);
            row.removeClass( \'empty-row screen-reader-text\' );
            row.insertBefore( \'#repeatable-fieldset-one tbody>tr:last\' );
            return false;
        });

        $( \'.remove-row\' ).on(\'click\', function() {
            $(this).parents(\'tr\').remove();
            return false;
        });
    });
    </script>

    <table id="repeatable-fieldset-one" width="100%">

    <thead>
        <tr>
            <th width="15%">Reward Amount</th>
            <th width="25%">Reward Title</th>
            <th width="10%">Shipping</th>
            <th width="20%">date</th>                  
            <th width="25%">Reward Description</th>
            <th width="5%"></th>                    
        </tr>
    </thead>

    <tbody>

<?php

    ( $reward_value );

    foreach ( $reward_value as $field ) {

?>

    <tr>
        <td><input type="text" class="widefat" name="reward[]" value="<?php if ( isset ( $field[\'reward\'] ) ) echo esc_attr( $field[\'reward\'] ); ?>" pattern="[1-9]\\d*" /></td>

        <td><input type="text" class="widefat" name="reward_title[]" value="<?php if ( isset ( $field[\'reward_title\'] ) ) echo esc_attr( $field[\'reward_title\'] ); ?>" /></td>

        <td>
            <select class="widefat" name="reward_shipping[]">
            <?php foreach ( $options as $label => $value ) : ?>
            <option value="<?php echo $value; ?>"<?php selected( $field[\'reward_shipping\'], $value ); ?>><?php echo $label; ?></option>
            <?php endforeach; ?>
            </select>
        </td>

        <script type="text/javascript">
            jQuery(document).ready(function($){
    $(\'.my-custom-datepicker-field\').datepicker({
        dateFormat: \'dd-mm-yy\', //maybe you want something like this
        showButtonPanel: true
    });
});
        </script>

        <td><input type="text" name="_custom_datepicker[]" id="_custom_datepicker[]" class="my-custom-datepicker-field" value="<?php echo esc_attr( $field[\'reward_date\'] ); ?>" /></td>

        <td><textarea class="widefat" name="reward_description[]"><?php if ( isset ( $field[\'reward_description\'] ) ) echo esc_attr( $field[\'reward_description\'] ); ?></textarea></td>

        <td><input type="image" class="remove-row" src="<?php bloginfo(\'template_directory\'); ?>/assets/images/remove-icon.png" alt="Submit"  width="35" height="35"></td>
    </tr>

<?php } ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><input type="text" class="widefat" name="reward[]" /></td>

        <td><input type="text" class="widefat" name="reward_title[]" /></td>

        <td>
            <select class="widefat" name="reward_shipping[]">
            <?php foreach ( $options as $label => $value ) : ?>
            <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
            <?php endforeach; ?>
            </select>
        </td>

        <script type="text/javascript">
            jQuery(document).ready(function($){
    $(\'.my-custom-datepicker-field\').datepicker({
        dateFormat: \'dd-mm-yy\', //maybe you want something like this
        showButtonPanel: true
    });
});
        </script>        

        <td><input type="text" class="my-custom-datepicker-field" name="_custom_datepicker[]" /></td>          

        <td><textarea class="widefat" name="reward_description[]" ></textarea></td>

        <td><input type="image" class="remove-row" src="<?php bloginfo(\'template_directory\'); ?>/assets/images/remove-icon.png" alt="Submit"  width="35" height="35"></td>
    </tr>

    </tbody>

    </table>

    <p><a id="add-row" class="button" href="#">Add Reward</a></p>

<?php   

}

function save_project_reward( $post_id ) {

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

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'project_reward_nonce\'], \'project_reward_nonce\' ) ) {
        return;
    }

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

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {

        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }

    }
    else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    $reward_data = array();
    $options = project_rewards_select_options();

    $rewards = $_POST[\'reward\'];
    $reward_titles = $_POST[\'reward_title\'];
    $reward_shippings = $_POST[\'reward_shipping\'];
    $reward_dates = $_POST[\'reward_date\'];    
    $reward_descriptions = $_POST[\'reward_description\'];

    $count = count( $rewards );
    for ( $i = 0; $i < $count; $i++ ) {

        if ( $rewards[$i] != \'\' ) :
            $reward_data[$i][\'reward\'] = stripslashes( strip_tags( $rewards[$i] ) );

        if ( in_array( $reward_shippings[$i], $options ) )
            $reward_data[$i][\'reward_shipping\'] = stripslashes( strip_tags( $reward_shippings[$i] ) );
                else
            $reward_data[$i][\'reward_shipping\'] = \'\';
endif;

        if ( $reward_titles[$i] != \'\' ) :
            $reward_data[$i][\'reward_title\'] = stripslashes( strip_tags( $reward_titles[$i] ) );
endif;

        if ( $reward_descriptions[$i] != \'\' ) :
            $reward_data[$i][\'reward_description\'] = stripslashes( $reward_descriptions[$i] );
endif;

        if ( $reward_descriptions[$i] != \'\' ) :
            $reward_data[$i][\'reward_date\'] = stripslashes( $reward_dates[$i] );
endif;            

    }

    update_post_meta( $post_id, \'_project_rewards\', $reward_data );

}

add_action( \'save_post\', \'save_project_reward\' );

结束