简体   繁体   English

ACF 字段未更新

[英]ACF field not updated

I created an ACF field Text named 'user-dir' linked to the table 'users'.我创建了一个名为“user-dir”的 ACF 字段文本,链接到表“用户”。

  • 'User Role' is equal to 'All' “用户角色”等于“全部”
  • 'User Form' is equal to 'Add/Edit' “用户表单”等于“添加/编辑”

Normally, the ACF field is updated when user registers.通常,ACF 字段会在用户注册时更新。 That does not.事实并非如此。

The belowed code must:下面的代码必须:

  • create a directory with user_id => that works fine, the directory is created使用 user_id => 创建一个工作正常的目录,该目录已创建
  • set the value of the ACF field 'user-dir' to 'bla bla bla' => that does not work and the ACF field 'user-dir' is not updated.将 ACF 字段 'user-dir' 的值设置为 'bla bla bla' => 这不起作用,并且 ACF 字段 'user-dir' 未更新。

Could you help me please to understand what is wrong.你能帮我理解什么是错的。

function create_user_dir($user_id) {
   $upload_dir = wp_upload_dir();
   wp_mkdir_p( $upload_dir['basedir'] . '/' . $user_id );
   $userdir = 'bla bla bla';
   update_field( 'user-dir', $userdir, $user_id );
}
add_action( 'user_register', 'create_user_dir');

You are updating the field - but on the wrong object.您正在更新该字段 - 但在错误的 object 上。

See https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects - to update an ACF value on a user , you need to prefix the user id with user_ ,请参阅https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects - 要更新用户的 ACF 值,您需要在用户ID 前加上user_前缀,

$post_id = "user_2"; // user ID = 2

So in your above code, this should be所以在你上面的代码中,这应该是

update_field( 'user-dir', $userdir, 'user_' . $user_id );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM