我知道我应该使用update\\u post\\u meta()函数,但不知道在代码中放在哪里。我尝试了不同的方法,但它不工作,它应该如何。
if ( ! function_exists( \'noo_render_location_picker_field\' ) ) :
function noo_render_location_picker_field( $field = array(), $field_id = \'\', $value = \'\', $form_type = \'\', $object = array() ) {
wp_enqueue_script( \'location-picker\' );
$post_id = ! empty( $object ) ? $object[\'ID\'] : 0;
$input_id = $field_id;
$class = isset( $field[\'required\'] ) && $field[\'required\'] ? \'class="noo-mb-location-address form-control jform-validate" required aria-required="true"\' : \' class="noo-mb-location-address form-control"\';
$address = noo_get_post_meta( $post_id, $field_id, \'\' );
$lat = noo_get_post_meta( $post_id, $field_id . \'_lat\', \'\' );
$lon = noo_get_post_meta( $post_id, $field_id . \'_lon\', \'\' );
$is_frontend_submit = ( isset( $_GET[\'action\'] ) && $_GET[\'action\'] == \'post_job\' ) ? true : false;
$is_edit_job = ( isset( $_GET[\'job_id\'] ) && ! empty( $_GET[\'job_id\'] ) ) ? true : false;
$checkbox_label = __( \'The same as company address\', \'noo\' );
$checkbox_id = \'_use_company_address\';
$company_id = jm_get_employer_company();
$full_address_company = ! empty( $company_id ) ? get_post_meta( $company_id, \'_full_address\', true ) : \'\';
$checkbox_value = empty( $post_id ) ? 1 : get_post_meta( $post_id, $checkbox_id, true );
if ( ($is_frontend_submit && !empty($full_address_company) ) or (!empty($full_address_company) && $is_edit_job)):
?>
<input name="<?php echo $checkbox_id; ?>" type="hidden" value="0"/>
<div class="form-control-flat">
<label class="checkbox">
<input id="use_company_address" name="<?php echo $checkbox_id; ?>" type="checkbox" <?php checked( $checkbox_value ); ?> value="1"/><i></i>
<?php echo esc_html( $checkbox_label ); ?>
</label>
</div>
<script>
jQuery(document).ready(function () {
jQuery("#use_company_address").change(function () {
if (jQuery(this).is(":checked")) {
jQuery(".noo-location-picker-field-wrap").addClass(\'hidden\');
} else {
jQuery(".noo-location-picker-field-wrap").removeClass(\'hidden\');
}
}).change();
});
</script>
<?php endif; ?>
<div class="noo-location-picker-field-wrap <?php echo $checkbox_value ? \'\' : \'\'; ?>">
<input <?php echo $class; ?> type="text" name="<?php echo esc_attr( $input_id ); ?>" value="<?php echo esc_html($address); ?>" placeholder="<?php echo esc_html__(\'Enter an exact address.\', \'noo\') ?>" />
<input type="hidden" class="noo-mb-lat" name="<?php echo esc_attr( $input_id ) ?>_lat" value="<?php echo $lat; ?>">
<input type="hidden" class="noo-mb-lon" name="<?php echo esc_attr( $input_id ) ?>_lon" value="<?php echo $lon; ?>">
<div class="noo-mb-job-location" id="<?php echo esc_attr( $input_id ); ?>" data-lat="<?php echo $lat; ?>"
data-lon="<?php echo $lon; ?>" style="height: 300px;">
</div>
</div>
<?php
}
endif;
SO网友:Cleverson Franco
如果我理解正确,则无法使用update\\u post\\u meta进行保存。如果是这样的话,您可以尝试使用ajax使用jQuery保存,试试这个。
//on change checkbox
jQuery.ajax({
type: \'POST\',
url: "/wp-admin/admin-ajax.php?action=your_custom_function",
data:{
meta_id: \'YOUR_META_ID\', //get user or post meta and set here
meta_value: \'YOUR_META_VALUE\' /the value you want to save in the meta
},
dataType: \'JSON\',
success: function(res)
{
console.log(res);
}
});
//in your functions.php
function your_custom_function(){
return (get_post_meta($_POST[\'meta_id\'], \'you_meta_name\')) ? update_post_meta($_POST[\'meta_id\'], \'you_meta_name\', $_POST[\'meta_value\']) : ($_POST[\'meta_id\'], \'you_meta_name\', $_POST[\'meta_value\'], true);
}
add_action(\'wp_ajax_your_custom_function\',\'your_custom_function\');
因此,每次更改复选框时,都会更新您的目标,您可以使用控制台的XHR对其进行调试。
我希望这对你有帮助。