【发布时间】:2020-03-30 03:03:57
【问题描述】:
我有一个表单设置供用户在我的 WordPress 网站上注册后填写。我使用 Contact Form 7 设置了表单,并有一个名为 radio-766 的单选按钮,它有两个选项:订阅者和客户。
我希望用户选择这两个选项之一,然后当他们提交表单时,它将更改他们的用户角色。
以下是我目前所拥有的...我从网上获取了 sn-ps 并尝试创建自己的,但它不起作用。关于如何让它发挥作用的任何想法?
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表单没有任何联系,所以我找到了这个sn-p的代码,只是不知道如何集成和工作
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') {
【问题讨论】:
-
确保您的函数 tm_cf7_roles_posted_data 在表单提交后正在调用?
-
你能在这里打印$role吗
-
嘿@ajith 和 jinesh 感谢您的回复......我到底是怎么做这些事情的?我不确定要添加什么
-
@Kiki 你能看看这个帖子吗stackoverflow.com/questions/40000543/…
-
感谢@ajith,我现在也尝试使用该帖子中的一些信息,但它也没有工作。如果有帮助的话,我刚刚用更多信息更新了我的问题
标签: php wordpress contact-form-7