Php 联系表单7(CF7)单选按钮以更改WordPress上的用户角色
我有一个表单设置供用户在我的WordPress网站注册后填写。我使用联系人表单7设置表单,并有一个单选按钮,名为radio-766,有两个选项:订户和客户 我希望用户从这两个选项中选择一个,然后当他们提交表单时,它将更改他们的用户角色 下面是我到目前为止所拥有的。。。我从网上抓取了一些片段,并试图创建自己的片段,但它不起作用。有什么办法可以让它工作吗Php 联系表单7(CF7)单选按钮以更改WordPress上的用户角色,php,wordpress,contact-form-7,Php,Wordpress,Contact Form 7,我有一个表单设置供用户在我的WordPress网站注册后填写。我使用联系人表单7设置表单,并有一个单选按钮,名为radio-766,有两个选项:订户和客户 我希望用户从这两个选项中选择一个,然后当他们提交表单时,它将更改他们的用户角色 下面是我到目前为止所拥有的。。。我从网上抓取了一些片段,并试图创建自己的片段,但它不起作用。有什么办法可以让它工作吗 function tm_cf7_roles_posted_data( $posted_data ) { // Stop if user
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吗?找不到有关此的信息
非常感谢您的帮助
编辑
我觉得这个函数与名为“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') {
至于以编程方式设置用户角色,只要定义了该角色,您就可以使用user对象并使用set_role()函数将其角色更改为您想要的任何角色
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' );
根据联系表单7插件作者
是\u用户\u登录()
将在表单提交中处理以下案例:
subscribers\u only:true
在表单中的附加设置中设置或add\u过滤器('WPCF7\u VERIFY\u NONCE','uu return\u true'),将WPCF7\u VERIFY\u NONCE设置为true代码>
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\u role
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );
请确保您的函数tm\u cf7\u roles\u posted\u data在表单提交后正在调用?您能在此处打印$role吗?Hey@ajith和jinesh感谢您的回复。。。我该怎么做这些事情呢?我不知道该怎么办add@Kiki你能查一下这篇帖子吗?谢谢@ajith,我现在也试着使用那篇帖子中的一些信息,但也没用。我刚刚更新了我的问题,补充了一些信息,如果这有帮助的话,谢谢你,但是这也不起作用:(..我认为在关联CF7表单中的无线电选项时仍然缺少一些内容。你能打印$role吗?这样我就可以知道你是否得到了正确的值?请检查我的答案我已经更新了,也许这会有所帮助…这是我从原始帖子中获得的详细信息:这可以正确工作并更改角色,现在是我尝试将select更改为CF7,使其不起作用。是否可以共享您的网站url?echo$role=sanitize_key($\u POST['radio-766']);不幸的是,它不是本地托管的。当我打印此代码中的角色时,没有显示任何内容。我将使用我找到的另一段直接连接到CF7表单的代码更新我的问题