You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ACF字段中记录修改会员资料的操作人信息并修复代码后台失效问题

如何在ACF字段中记录修改会员资料的操作人信息并修复代码后台失效问题

Hey there! Let's break down why your current code isn't working and fix it to get exactly what you need.

First, let's address the issues in your existing code:

  • The edit_user_profile and show_user_profile hooks only pass one parameter: the $user object of the member whose profile is being edited. Your function is expecting two parameters ($member_id, $user_id), which means those variables are either undefined or holding incorrect data.
  • You're using update_field when the profile page loads, which saves the value to the database immediately—even if the admin just views the profile without clicking "Update Profile". That's probably not what you want, since it would overwrite the field every time someone opens the page.

Now, let's look at two tailored solutions depending on your exact needs:


Solution 1: Pre-fill the ACF field with the editor's name (visible before hitting update)

This approach dynamically sets the field's value in the edit form so the admin sees their display name as soon as they open the profile. The value only gets saved to the database when they click "Update Profile".

Use ACF's acf/load_value filter to populate the field when the form loads:

function load_responsible_editor_name( $value, $post_id, $field ) {
    // Check if we're on a user profile edit page
    if ( strpos( $post_id, 'user_' ) === 0 ) {
        // Get the current logged-in admin/moderator editing the profile
        $current_editor = wp_get_current_user();
        
        if ( $current_editor instanceof WP_User ) {
            // Return the editor's display name as the field value
            return $current_editor->display_name;
        }
    }
    
    // Fallback to the existing value if not on a user profile page
    return $value;
}
// Replace with your ACF field key
add_filter( 'acf/load_value/key=field_673bbd254e89a', 'load_responsible_editor_name', 10, 3 );

Solution 2: Record the editor's name only when the profile is saved

If you want to ensure the field always reflects who actually saved the profile (even if someone manually edits the field value), use the profile save hooks instead. This updates the field in the database immediately when the profile is saved:

function save_responsible_editor_on_update( $user_id ) {
    // Get the current logged-in admin/moderator who saved the profile
    $current_editor = wp_get_current_user();
    
    if ( $current_editor instanceof WP_User ) {
        // Update the ACF field with the editor's display name
        update_field( 'field_673bbd254e89a', $current_editor->display_name, 'user_' . $user_id );
    }
}
// Hook into both profile update actions
add_action( 'edit_user_profile_update', 'save_responsible_editor_on_update' );
add_action( 'personal_options_update', 'save_responsible_editor_on_update' );

Quick Notes:

  • If you want to store the editor's user_id instead of their display name, just replace $current_editor->display_name with $current_editor->ID.
  • Keep your ACF field key (field_673bbd254e89a) intact—it ensures the filter/hook targets the exact field you want.

备注:内容来源于stack exchange,提问作者iMarkDesigns

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:38:00