我安装了 buddypress 主题,允许处理 WPJM 插件以进行工作和简历注册。对于后者,我需要用户注册为候选人或雇主,以控制谁可以查看简历或发布职位等。
但是主题没有按照标准提供此功能,但他们的支持非常友善地为我提供了建议的自定义代码来处理此问题。我无法让它工作,想知道是否有人知道可能缺少什么?
这是他们为这个问题写给我的:
Create a Profile Field for “Role”: Set options like “Employer” and “Candidate” for users to select during registration.
Use Code to Assign the Role: A code snippet can assign the correct role based on the user’s selection in this profile field.
Please let us know if you’d like guidance on the custom code snippet to achieve this or if you’d prefer assistance from a developer.
Here is a code snippet you can use to assign a user role based on the value selected in a custom BuddyPress profile field during registration. This example assumes the profile field is named “Role” and has values such as “Employer” or “Candidate.”
function assign_role_based_on_profile_field($user_id) {
// Get the BuddyPress profile data for the registered user
if (function_exists('xprofile_get_field_data')) {
$role_value = xprofile_get_field_data('Role', $user_id); // Replace 'Role' with the exact name of your profile field
// Assign the user role based on the profile field value
if ($role_value == 'Employer') {
// Assign 'employer' role
$user = new WP_User($user_id);
$user->set_role('employer'); // Replace 'employer' with the exact role slug
} elseif ($role_value == 'Candidate') {
// Assign 'candidate' role
$user = new WP_User($user_id);
$user->set_role('candidate'); // Replace 'candidate' with the exact role slug
} else {
// Default role if no specific selection is made
$user = new WP_User($user_id);
$user->set_role('subscriber'); // Or any other default role
}
}
}
add_action('bp_core_signup_user', 'assign_role_based_on_profile_field', 10, 1);
Make sure the roles employer and candidate are defined in your site.
我想使用 WPJM 用户角色作为标准:雇主和候选人。不确定这里的鼻涕虫是否只是正确的“雇主”和“候选人”?
我已经询问了 Gemini 和 chatgpt,他们似乎确认这是正确的。但它不起作用 - 所以有些东西不正确,我错过了一些东西。
致以诚挚的问候,
弗拉穆伦
您能否确认给定的角色是否存在?如果没有,您可以使用下面的代码创建角色
// Register custom roles if they don't already exist
function register_custom_wpjm_roles() {
if ( ! get_role( 'employer' ) ) {
add_role( 'employer', __( 'Employer' ), array( 'read' => true ) );
}
if ( ! get_role( 'candidate' ) ) {
add_role( 'candidate', __( 'Candidate' ), array( 'read' => true ) );
}
}
add_action( 'init', 'register_custom_wpjm_roles' );
BuddyPress 操作挂钩 bp_core_signup_user 在用户注册时但在其帐户完全激活之前触发。对于您的情况,使用 bp_core_activated_user 挂钩通常更可靠,该挂钩在帐户激活后触发,并且在 BuddyPress 注册中效果更好。
add_action( 'bp_core_activated_user', 'assign_role_based_on_profile_field', 10, 1 );
下面是完整的代码,但在此之前请确保配置文件字段在 BuddyPress 中准确命名为“Role”(区分大小写)。您可以通过导航到 WordPress 管理员中的“用户”>“个人资料字段”来仔细检查这一点。如果名称不同,请更新 xprofile_get_field_data 函数中的字符串。
function assign_role_based_on_profile_field($user_id) {
// Ensure BuddyPress and xProfile are active before running
if (function_exists('xprofile_get_field_data') && $user_id) {
// Get the custom 'Role' field value
$role_value = xprofile_get_field_data('Role', $user_id); // Ensure 'Role' matches the exact field name in BuddyPress
if ( $role_value ) {
$user = new WP_User($user_id);
// Assign roles based on the profile field value
if ($role_value === 'Employer') {
$user->set_role('employer'); // Assumes 'employer' role exists
} elseif ($role_value === 'Candidate') {
$user->set_role('candidate'); // Assumes 'candidate' role exists
} else {
$user->set_role('subscriber'); // Fallback role
}
} else {
error_log("Role value is empty or invalid for user ID: $user_id");
}
} else {
error_log("xProfile not available or invalid user ID: $user_id");
}
}
add_action( 'bp_core_activated_user', 'assign_role_based_on_profile_field', 10, 1 );