Contact Form 7 (CF7) 单选按钮以更改 WordPress 上的用户角色
Contact Form 7 (CF7) radio button to change User Role on WordPress
我有一个表单设置供用户在我的 WordPress 网站上注册后填写。我使用 Contact Form 7 设置了表单,并有一个名为 radio-766 的单选按钮,它有两个选项:订阅者和客户。
我希望用户选择这两个选项之一,然后当他们提交表单时,它会改变他们的用户角色。
以下是我目前所拥有的...我从网上抓取了片段并尝试创建自己的片段,但它不起作用。关于如何让它工作的任何想法?
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $posted_data['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
我应该在任何地方包含表单名称或 ID 吗?找不到关于此的信息
非常感谢任何帮助!
编辑
我觉得这个函数与名为 "After LinkedIn" 的 CF7 表单没有任何联系,所以我找到了这段代码,只是不确定如何集成并开始工作
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
// Contact Form 7 version 3.9 removed $cfdata->posted_data and now
// we have to retrieve it from an API
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
// For pre-3.9 versions of Contact Form 7
$formdata = $cfdata->posted_data;
} else {
// We can't retrieve the form data
return $cfdata;
}
// Check this is the user registration form
if ( $cfdata->title() == 'After LinkedIn') {
至于以编程方式设置用户角色,您只需获取用户对象并使用 set_role() 函数将他们的角色更改为您想要的任何角色,只要该角色已 defined.Try这样
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $_POST['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->set_role($role);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
根据 Contact form 7 插件作者 is_user_logged_in()
将处理以下表单提交案例:
subscribers_only: true
在表单的附加设置中设置。 或
- 使用
add_filter( 'wpcf7_verify_nonce', '__return_true' );
将 WPCF7_VERIFY_NONCE
设置为真
更多信息click here。
此外,要更改用户角色,您可以执行以下操作:
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
function tm_cf7_roles_posted_data( $posted_data ) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$formID = $wpcf7->id();
if ( $submission ) {
if( $formID == "YOUR-FORM-ID" ) {
if ( is_user_logged_in() ) {
$role = sanitize_key( $posted_data['radio-766'][0] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
// Getting the WP_User object
$user = wp_get_current_user();
// The user exists
if( $user && $user->exists() ) {
// Check if user already has that role or not
if ( !in_array( $role, (array) $user->roles ) ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( $role );
}
}
}
}
}
}
如果您只想向用户添加新角色并保留现有角色,请使用下面的 set_role
而不是:
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );
我有一个表单设置供用户在我的 WordPress 网站上注册后填写。我使用 Contact Form 7 设置了表单,并有一个名为 radio-766 的单选按钮,它有两个选项:订阅者和客户。
我希望用户选择这两个选项之一,然后当他们提交表单时,它会改变他们的用户角色。
以下是我目前所拥有的...我从网上抓取了片段并尝试创建自己的片段,但它不起作用。关于如何让它工作的任何想法?
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $posted_data['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
我应该在任何地方包含表单名称或 ID 吗?找不到关于此的信息
非常感谢任何帮助!
编辑
我觉得这个函数与名为 "After LinkedIn" 的 CF7 表单没有任何联系,所以我找到了这段代码,只是不确定如何集成并开始工作
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
// Contact Form 7 version 3.9 removed $cfdata->posted_data and now
// we have to retrieve it from an API
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
// For pre-3.9 versions of Contact Form 7
$formdata = $cfdata->posted_data;
} else {
// We can't retrieve the form data
return $cfdata;
}
// Check this is the user registration form
if ( $cfdata->title() == 'After LinkedIn') {
至于以编程方式设置用户角色,您只需获取用户对象并使用 set_role() 函数将他们的角色更改为您想要的任何角色,只要该角色已 defined.Try这样
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $_POST['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->set_role($role);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
根据 Contact form 7 插件作者 is_user_logged_in()
将处理以下表单提交案例:
subscribers_only: true
在表单的附加设置中设置。 或- 使用
add_filter( 'wpcf7_verify_nonce', '__return_true' );
将
WPCF7_VERIFY_NONCE
设置为真
更多信息click here。
此外,要更改用户角色,您可以执行以下操作:
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
function tm_cf7_roles_posted_data( $posted_data ) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$formID = $wpcf7->id();
if ( $submission ) {
if( $formID == "YOUR-FORM-ID" ) {
if ( is_user_logged_in() ) {
$role = sanitize_key( $posted_data['radio-766'][0] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
// Getting the WP_User object
$user = wp_get_current_user();
// The user exists
if( $user && $user->exists() ) {
// Check if user already has that role or not
if ( !in_array( $role, (array) $user->roles ) ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( $role );
}
}
}
}
}
}
如果您只想向用户添加新角色并保留现有角色,请使用下面的 set_role
而不是:
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );