【创建没有插件的”
中继器字段”
】我想创建一个中继器元框而不使用任何类型的插件。
如何在”
选定模板”
中获取此metabox?
#1
add_action('admin_init', 'gpm_add_meta_boxes', 2);
function gpm_add_meta_boxes() {
add_meta_box( 'gpminvoice-group', 'Custom Repeatable', 'Repeatable_meta_box_display', 'page', 'normal', 'default');
}function Repeatable_meta_box_display() {
global $post;
$gpminvoice_group = get_post_meta($post->
ID, 'customdata_group', true);
wp_nonce_field( 'gpm_repeatable_meta_box_nonce', 'gpm_repeatable_meta_box_nonce' );
?>
<
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%">
<
tbody>
<
?php
if ( $gpminvoice_group ) :
foreach ( $gpminvoice_group as $field ) {
?>
<
tr>
<
td width="15%">
<
input type="text"placeholder="Title" name="TitleItem[]" value="http://www.srcmini.com/<
?php if($field['TitleItem'] != '') echo esc_attr( $field['TitleItem'] );
?>
" />
<
/td>
<
td width="70%">
<
textarea placeholder="Description" cols="55" rows="5" name="TitleDescription[]">
<
?php if ($field['TitleDescription'] != '') echo esc_attr( $field['TitleDescription'] );
?>
<
/textarea>
<
/td>
<
td width="15%">
<
a class="button remove-row" href="http://www.srcmini.com/#1">
Remove<
/a>
<
/td>
<
/tr>
<
?php
}
else :
// show a blank one
?>
<
tr>
<
td>
<
input type="text" placeholder="Title" title="Title" name="TitleItem[]" />
<
/td>
<
td>
<
textareaplaceholder="Description" name="TitleDescription[]" cols="55" rows="5">
<
/textarea>
<
/td>
<
td>
<
a class="buttoncmb-remove-row-button button-disabled" href="http://www.srcmini.com/#">
Remove<
/a>
<
/td>
<
/tr>
<
?php endif;
?>
<
!-- empty hidden one for jQuery -->
<
tr class="empty-row screen-reader-text">
<
td>
<
input type="text" placeholder="Title" name="TitleItem[]"/>
<
/td>
<
td>
<
textarea placeholder="Description" cols="55" rows="5" name="TitleDescription[]">
<
/textarea>
<
/td>
<
td>
<
a class="button remove-row" href="http://www.srcmini.com/#">
Remove<
/a>
<
/td>
<
/tr>
<
/tbody>
<
/table>
<
p>
<
a id="add-row" class="button" href="http://www.srcmini.com/#">
Add another<
/a>
<
/p>
<
?php
}
add_action('save_post', 'custom_repeatable_meta_box_save');
function custom_repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST['gpm_repeatable_meta_box_nonce'] ) ||
! wp_verify_nonce( $_POST['gpm_repeatable_meta_box_nonce'], 'gpm_repeatable_meta_box_nonce' ) )
return;
if (defined('DOING_AUTOSAVE') &
&
DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'customdata_group', true);
$new = array();
$invoiceItems = $_POST['TitleItem'];
$prices = $_POST['TitleDescription'];
$count = count( $invoiceItems );
for ( $i = 0;
$i <
$count;
$i++ ) {
if ( $invoiceItems[$i] != '' ) :
$new[$i]['TitleItem'] = stripslashes( strip_tags( $invoiceItems[$i] ) );
$new[$i]['TitleDescription'] = stripslashes( $prices[$i] );
// and however you want to sanitize
endif;
}
if ( !empty( $new ) &
&
$new != $old )
update_post_meta( $post_id, 'customdata_group', $new );
elseif ( empty($new) &
&
$old )
delete_post_meta( $post_id, 'customdata_group', $old );
}
推荐阅读
- 在自定义帖子类型metabox上创建复选框字段
- 计算用户点击WordPress中的按钮的次数
- 纠正WordPress CSS中的边距/填充
- 容器缩放比例不小于576px
- Spring Boot指标监控与健康检查
- k8s安全
- [设计模式系列] 建造者
- 源码解读Dubbo分层设计思想
- 华为三面(说说ListMap和Set有什么区别())