如何从首页更新用户配置文件中的自定义字段复选框 ACF
How update custom field checkbox ACF in user profile from front page
我制作了一组 Checkbox 类型的字段,以便能够使用 ACF select 多个选项 |高级自定义字段,这些字段被添加到 WP 仪表板中的用户配置文件中,我想使用自定义表单从前端编辑它们。
创建的字段:
用户个人资料中的字段:
首页表格
我需要当你 select 主页页面上的任何选项/复选框已保存并在用户配置文件中更新。
我在 from 模板中的代码是这样的:
account_page.php
<form id="your-profile" class="nice" action="<?php echo the_permalink() ?>" method="POST" />
<div>
<p>Please check the email newsletters you wish to receive.<br>Unchecking a box will unsubscribe you from future emails.</p>
</div>
<fieldset>
<input name="_wp_http_referer" value="<?php echo home_url('account') ?>" type="hidden">
<input name="from" value="profile" type="hidden">
<input name="action" value="update" type="hidden">
<input name="user_id" id="user_id" value="<?php echo $current_user->ID ?>" type="hidden">
<input name="checkuser_id" value="<?php echo get_current_user_id(); ?>" type="hidden">
<input name="nickname" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="user_email" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="phone" value="<?php echo $current_user->phone; ?>" type="hidden">
<input name="first_name" value="<?php echo $current_user->first_name ?>" type="hidden">
<input name="last_name" value="<?php echo $current_user->last_name ?>" type="hidden">
<input name="address" value="<?php echo $current_user->address ?>" type="hidden">
<input name="address2" value="<?php echo $current_user->address2 ?>" type="hidden">
<input name="city" value="<?php echo $current_user->city; ?>" type="hidden">
<input name="state" value="<?php echo $current_user->state; ?>" type="hidden">
<input name="zip_code" value="<?php echo $current_user->zip_code; ?>" type="hidden">
<input name="country" value="<?php echo $current_user->country; ?>" type="hidden">
<?php
//Get all items from newslwtter
$newsletter = get_field('newsletter_preferences', $current_user);
$id = get_the_ID();
$field = get_field_objects($current_user);
$field_name = $field["newsletter_preferences"]["name"];
if( $field ){
foreach ( $field["newsletter_preferences"]['choices'] as $key => $value) {
if ( in_array( $key, $newsletter ) ) {
$checked = 'checked';
}else{
$checked = '';
}
echo '<p class="field"><input type="checkbox" '.$checked.' name="'.$field_name.'" value="'.$key.'">'.$value.'</p>';
}
}
?>
</fieldset>
<div>
<input class="btn btn-primary" name="Update E-mail Preferences" value="Update E-mail Preferences" type="submit">
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="update-user">
</div>
</form>
并且在 functions.php
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['newsletter_preferences'] ) )
update_user_meta( $user_id, 'newsletter_preferences',
$_POST['newsletter_preferences'] );
}
add_action('personal_options_update','update_extra_profile_fields');
但是不要工作,不要保存任何东西,请帮助我。谢谢
已解决创建问题:
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['state'] ) )
$state = update_user_meta( $user_id, 'state', $_POST['state'] );
.
.
.
//all fields to save
}
add_action('personal_options_update', __NAMESPACE__ . '\update_extra_profile_fields');
我制作了一组 Checkbox 类型的字段,以便能够使用 ACF select 多个选项 |高级自定义字段,这些字段被添加到 WP 仪表板中的用户配置文件中,我想使用自定义表单从前端编辑它们。
创建的字段:
用户个人资料中的字段:
首页表格
我需要当你 select 主页页面上的任何选项/复选框已保存并在用户配置文件中更新。
我在 from 模板中的代码是这样的: account_page.php
<form id="your-profile" class="nice" action="<?php echo the_permalink() ?>" method="POST" />
<div>
<p>Please check the email newsletters you wish to receive.<br>Unchecking a box will unsubscribe you from future emails.</p>
</div>
<fieldset>
<input name="_wp_http_referer" value="<?php echo home_url('account') ?>" type="hidden">
<input name="from" value="profile" type="hidden">
<input name="action" value="update" type="hidden">
<input name="user_id" id="user_id" value="<?php echo $current_user->ID ?>" type="hidden">
<input name="checkuser_id" value="<?php echo get_current_user_id(); ?>" type="hidden">
<input name="nickname" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="user_email" value="<?php echo $current_user->user_email ?>" type="hidden">
<input name="phone" value="<?php echo $current_user->phone; ?>" type="hidden">
<input name="first_name" value="<?php echo $current_user->first_name ?>" type="hidden">
<input name="last_name" value="<?php echo $current_user->last_name ?>" type="hidden">
<input name="address" value="<?php echo $current_user->address ?>" type="hidden">
<input name="address2" value="<?php echo $current_user->address2 ?>" type="hidden">
<input name="city" value="<?php echo $current_user->city; ?>" type="hidden">
<input name="state" value="<?php echo $current_user->state; ?>" type="hidden">
<input name="zip_code" value="<?php echo $current_user->zip_code; ?>" type="hidden">
<input name="country" value="<?php echo $current_user->country; ?>" type="hidden">
<?php
//Get all items from newslwtter
$newsletter = get_field('newsletter_preferences', $current_user);
$id = get_the_ID();
$field = get_field_objects($current_user);
$field_name = $field["newsletter_preferences"]["name"];
if( $field ){
foreach ( $field["newsletter_preferences"]['choices'] as $key => $value) {
if ( in_array( $key, $newsletter ) ) {
$checked = 'checked';
}else{
$checked = '';
}
echo '<p class="field"><input type="checkbox" '.$checked.' name="'.$field_name.'" value="'.$key.'">'.$value.'</p>';
}
}
?>
</fieldset>
<div>
<input class="btn btn-primary" name="Update E-mail Preferences" value="Update E-mail Preferences" type="submit">
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="update-user">
</div>
</form>
并且在 functions.php
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['newsletter_preferences'] ) )
update_user_meta( $user_id, 'newsletter_preferences',
$_POST['newsletter_preferences'] );
}
add_action('personal_options_update','update_extra_profile_fields');
但是不要工作,不要保存任何东西,请帮助我。谢谢
已解决创建问题:
function update_extra_profile_fields($user_id) {
if ( isset( $_POST['state'] ) )
$state = update_user_meta( $user_id, 'state', $_POST['state'] );
.
.
.
//all fields to save
}
add_action('personal_options_update', __NAMESPACE__ . '\update_extra_profile_fields');