1

我有一个字段名为“sales_rep”的 ACF 选择字段。它填充有:

约翰·多·巴特·辛普森埃里克·卡特曼

该字段设置为显示在“用户表单”上,并且仅在“当前用户角色”是管理员时显示。

该字段显示在每个用户页面上,我可以手动将其更改为我想要的销售代表并保存。

问题是我希望能够在保存用户配置文件时以编程方式更新它。我最终会添加更多代码来确定销售代表,但我简化了这篇文章的问题。

下面是我在functions.php中的代码:

尝试 1:没有错误,也没有改变值

// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );


function update_rep( $user_id )
{
    update_user_meta( $user_id, 'sales_rep', 'Eric Cartman' );
}

尝试 2:没有错误,也没有改变值

// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );


function update_rep( $user_id )
{
    $field_key = "sales_rep";
    $value = array("Eric Cartman");
    update_field( $field_key, $value, $user_id );
}
4

2 回答 2

1

我认为@Andrii Kovalenko是对的,user_前缀可能是您的问题。

还注意到您将 acf 字段数据保存为array("Eric Cartman").

我假设您的 ACF 选择字段预定义的选择选项如下所示...

John Doe
Bart Simpson
Eric Cartman

这应该适用于您的 acf 选项...

// is run when you edit YOUR profile, and save it
add_action('personal_options_update', 'handle_user_profile_update' );

// is run when you edit ANY OTHER profile and save it
add_action('edit_user_profile_update', 'handle_user_profile_update' );

// on user profile update function handler
function handle_user_profile_update($user_id) {

    // update sales rep
    update_sales_rep_field($user_id);

    // add anymore on user update profile functions here...

}

// update sales rep field
function update_sales_rep_field($user_id) {

    // our values
    $value = 'Eric Cartman';

    // update acf user field by user id
    update_field('sales_rep', $value, 'user_' . $user_id);

}



出于好奇,这些销售代表实际上是您 wordpress 网站的用户吗?

如果是这样,您可以sales_rep像这样使用您网站上的销售代表用户动态填充选择用户字段...

function render_sales_rep_select_field($field) {
    
    // reset sales_rep select field choices
    $field['choices'] = [];

    // get users with user role sales
    $args = array(
        'role'    => 'sales', // your sales reps user role
        'orderby' => 'user_nicename',
        'order'   => 'ASC'
    );

    // get user with args from above
    $users = get_users( $args );

    // for each sales rep user
    foreach ( $users as $user ) {
        
        // build our sales rep select field choices by user_id > full name
        $field['choices'][$user->id] = $user->first_name . ' ' . $user->last_name
    
    }

    // return the select field
    return $field;
    
}

// load user field with our custom select drop of sales rep users
add_filter('acf/load_field/name=sales_rep', 'render_sales_rep_select_field');

使用此方法,您无需在用户将更改保存到他们的个人资料时更新该字段。

例如,如果你get_field喜欢这个...

// get user field sales rep array
get_field('sales_rep', 'user_' . $user_id ); // random user id

它将返回一个数组user_idfull name...

Array
    (
        [5] => Eric Cartman
    )

在您的 acf 用户选择字段设置中,您将需要选择return both值和标签。

于 2020-12-21T02:29:59.673 回答
1

似乎user_前缀可以解决问题。

尝试get/updateuser_前缀的字段。铁:

update_field( $field_key, $value, "user_$user_id" );

而对于update_user_meta,

之前调用 delete_user_meta 可以解决问题。

用户元可能有重复的键。

试试这个

delete_user_meta( $user_id, $field_key );
add_user_meta( $user_id, $field_key, $value );  
于 2020-12-20T20:34:59.163 回答