php [约会] - 其他自定义字段演示。有关如何在Appointments Lite中添加自定义字段的指南
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [约会] - 其他自定义字段演示。有关如何在Appointments Lite中添加自定义字段的指南相关的知识,希望对你有一定的参考价值。
<?php
/*
Plugin Name: [Appointments] - Custom Field Demo
Plugin URI: https://premium.wpmudev.org/
Description: Guide on how to add a custom field in Appointments Lite
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
add_filter('app_get_field_name', function( $field_names ){
$field_names['secondary-email'] = 'Secondary email';
return $field_names;
}, 10 );
add_filter('app_additional_fields', function( $form ){
$field = 'secondary-email';
$id = "appointments-{$field}" . md5( serialize( $field ) );
$field_label = 'Secondary email';
$value = '';
if( is_user_logged_in() ){
$value = get_user_meta( get_current_user_id(), 'app_' . $field, true );
}
ob_start();
?>
<div class="appointments-field appointments-<?php echo esc_attr( $field ); ?>-field">
<label for="<?php echo esc_attr( $id ); ?>"><span><?php echo esc_html( $field_label ); ?></span></label>
<input
type="text"
id="<?php echo esc_attr( $id ); ?>"
class="appointments-field-entry appointments-<?php echo esc_attr( $field ); ?>-field-entry secondary-email"
data-name="additional_fields[<?php echo esc_attr( $field ); ?>]"
name="<?php echo $field; ?>"
value="<?php echo esc_attr( $value ); ?>"
/>
</div>
<?php
$form .= ob_get_clean();
return $form;
}, 10 );
add_action('app-footer_scripts-after', function(){
?>
<script type="text/javascript">
(function( $ ) {
$( document ).ajaxSend( function( e, xhr, opts ) {
if ( ! opts.data ) {
return true;
}
if ( ! opts.data.match( /action=post_confirmation/ ) ) {
return true;
}
let el = $( ".secondary-email" );
opts.data += '&' + encodeURIComponent( "secondary-email" ) + '=' + encodeURIComponent( el.val() );
} );
})( jQuery );
</script>
<?php
}, 900);
add_action('app-appointments_list-edit-client', function( $deprecated, $app ){
$field = 'secondary-email';
$id = "appointments-{$field}" . md5( serialize( $field ) );
$field_label = 'Secondary email';
$value = appointments_get_appointment_meta( $app->ID, $field );
ob_start();
?>
<label for="additional_fields-<?php echo $field; ?>"><span class="title">Secondary email</span>
<span class='input-text-wrap'>
<input type="text" class="widefat appointments-field-entry additional_field"
data-name="<?php echo $field; ?>"
id="<?php echo $field; ?>"
value="<?php echo esc_attr( $value ); ?>" />
</span>
</label>
<br class="clear">
<script>
(function ($) {
$.ajaxSetup({
beforeSend: function (jqxhr, settings) {
if (!(settings && "data" in settings && settings.data.match(/action=inline_edit_save/))) return;
var matches = settings.data.match(/\bapp_id=(\d+)/),
app_id = matches && matches.length ? matches[1] : false,
root = app_id ? $(':hidden[name="app_id"][value="' + app_id + '"]').closest("tr") : $("body"),
fields = root.find(".appointments-field-entry.additional_field")
;
fields.each(function () {
var me = $(this),
name = me.attr("data-name"),
value = me.is(":checkbox") ? (me.is(":checked") ? 1 : 0) : me.val()
;
settings.data += '&' + encodeURIComponent(name) + '=' + encodeURIComponent(value);
});
}
});
})(jQuery);
</script>
<?php
echo ob_get_clean();
}, 10, 2);
add_action( 'wpmudev_appointments_insert_appointment', function( $app_id ){
$options = appointments_get_options();
if ( isset( $options['additional_fields-admin_edit'] ) ) {
$is_editable = (bool)$options['additional_fields-admin_edit'];
}
else {
$is_editable = true;
}
if ( ! $is_editable || ! appointments_get_appointment( $app_id ) ) {
return false;
}
appointments_update_appointment_meta( $app_id, 'secondary-email', wp_strip_all_tags( rawurldecode( $_REQUEST['secondary-email'] ) ) );
}, 2 );
add_action( 'wpmudev_appointments_update_appointment_result', function( $result, $app_id ){
$options = appointments_get_options();
if ( isset( $options['additional_fields-admin_edit'] ) ) {
$is_editable = (bool)$options['additional_fields-admin_edit'];
}
else {
$is_editable = true;
}
if ( ! $is_editable || ! appointments_get_appointment( $app_id ) ) {
return $result;
}
appointments_update_appointment_meta( $app_id, 'secondary-email', wp_strip_all_tags( rawurldecode( $_REQUEST['secondary-email'] ) ) );
}, 2, 2 );
以上是关于php [约会] - 其他自定义字段演示。有关如何在Appointments Lite中添加自定义字段的指南的主要内容,如果未能解决你的问题,请参考以下文章
php WordPress和ACF |如果菜单项具有自定义字段集,则添加类