如何从首页更新用户配置文件中的自定义字段复选框ACF

时间:2018-01-16 16:14:06

标签: php wordpress advanced-custom-fields

我创建了一组类型为Checkbox的字段,以便能够使用ACF |选择多个选项高级自定义字段,这些字段将添加到WP仪表板中用户的配置文件中,我想使用自定义表单从前端编辑它们。

已创建字段 Fields created

用户个人资料中的字段: Field inserted in user pro

首页上的表单 form front page

我在首页的页面上选择任何选项/复选框时需要保存并在用户的个人资料中更新。

我在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');

但是不要工作,不要保存任何东西,请帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

解决了创建:

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');