[英]Is there a way to update a user role and update ACF fields on the user profile in WordPress at the same time using WooCommerce actions?
我一直在嘗試將以下兩個功能結合起來,但只能成功執行其中一個,而不是同時執行這兩個功能。 我想知道是否有人知道可能導致沖突的原因。 這些是在通過WooCommerce注冊表和woocommerce_created_customer
操作創建客戶時執行的。
功能:1 (目的是更新用戶頁面上已經存在的兩個字段,感謝 ACF Pro)
function school_info_save( $customer_id ) {
if( isset( $_POST['school_name'] ) ) {
update_user_meta( $customer_id, 'school_name', sanitize_text_field($_POST['school_name']) );
}
if( isset( $_POST['school_email'] ) ) {
update_user_meta( $customer_id, 'school_email', sanitize_email($_POST['school_email']) );
}
}
add_action('woocommerce_created_customer', 'school_info_save', 20);
功能2:(目的是然后將用戶角色更新為學生)
function update_to_student_role( $customer_id ) {
if( isset( $_POST['school_name']) && isset( $_POST['school_email']) ) {
wp_update_user( array( 'ID' => $customer_id, 'role' => 'student' ) );
}
}
add_action('woocommerce_created_customer', 'update_to_student_role', 10);
我已經在不同的優先級嘗試過這兩種方法。 通常,如果兩者都處於活動狀態,則只有學生角色功能會成功。 只是想知道是否有人可以向我解釋為什么wp_update_user
阻止update_user_meta
工作 - 如果這確實發生了。
此外,我還嘗試在一個函數中運行上述所有功能,結果相同。
function school_info_save( $customer_id ) {
$metas = array(
'wp_capabilities' => array('student' => true),
'school_name' => sanitize_text_field($_POST['school_name']),
'school_email' => sanitize_email($_POST['school_email'])
);
if( isset( $_POST['school_name']) && isset( $_POST['school_email']) ) {
foreach($metas as $key => $value) {
update_user_meta( $customer_id, $key, $value );
}
}
}
add_action('woocommerce_created_customer', 'school_info_save', 20);
謝謝你。
您可以改為更改您的功能 2。像這樣:
function update_to_student_role( $new_customer_data ) {
if( isset( $_POST['school_name']) && isset( $_POST['school_email']) ) {
$new_customer_data['role'] = 'student';
}
return $new_customer_data;
}
add_action('woocommerce_new_customer_data', 'update_to_student_role');
沒關系......我忘記實際修改 ACF 字段規則,以便這兩個學生字段出現在具有“學生”角色的用戶配置文件中......功能正在運行,但字段不在那里接收我相信角色變化時的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.