我已经成功获取了Contact Form 7表单以创建新用户,该表单完美地向自定义元数据字段添加了详细信息,但是我现在想创建一个Edit Profile表单并希望能够更新用户元。
我该如何调整确切的代码来更新用户元而不是创建新用户?
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
function create_investor_from_registration($cfdata) {
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() == 'Investor Registration') {
$password = $formdata['password'];
$email = $formdata['your-email'];
$industry = isset($formdata['industryinterest'][0])?$formdata['industryinterest'][0]:'';
$stage = isset($formdata['menu-608'][0])?$formdata['menu-608'][0]:'';
$country = $formdata['country'];
$singlepooled = isset($formdata['radio-263'][0])?$formdata['radio-263'][0]:'';
$leadinvestor = isset($formdata['radio-264'][0])?$formdata['radio-264'][0]:'';
$amount = $formdata['amount-invest'];
$portfolio = $formdata['portfolio-examples'];
$about = $formdata['about-investor'];
$name = $formdata['firstname'];
$lastname = $formdata['lastname'];
// Construct a username from the user's name
$username = $formdata['your-email'];
if ( !email_exists( $email ) ) {
// Find an unused username
$username_tocheck = $username;
$i = 1;
while ( username_exists( $username_tocheck ) ) {
$username_tocheck = $username . $i++;
}
$username = $username_tocheck;
// Create the user
$userdata = array(
'user_login' => $email,
'user_pass' => $password,
'user_email' => $email,
'nickname' => reset($username),
'display_name' => $name,
'first_name' => $name,
'last_name' => $lastname,
'industryinterest' => $industry,
'stageinterest' => $stage,
'countryinterest' => $country,
'singlepooled' => $singlepooled,
'leadinvestor' => $leadinvestor,
'investamount' => $amount,
'portfolio' => $portfolio,
'about' => $about,
'role' => 'customer'
);
$user_id = wp_insert_user( $userdata );
if ( !is_wp_error($user_id) ) {
// Email login details to user
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = "Welcome! Your login details are as follows:" . "\r\n";
$message .= sprintf(__('Username: %s'), $username) . "\r\n";
$message .= sprintf(__('Password: %s'), $password) . "\r\n";
$message .= "NEW LOGIN URL HERE";
wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, false, is_ssl() );
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_investor_from_registration', 1);
下面是我尝试更改自己的代码段,但是它似乎没有用,我不确定为什么...我将其简化为“ about”字段,以方便地测试单个字段。
function update_investor_profile($cfdata) {
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() == 'Update Profile') {
if ( is_user_logged_in() ) {
$about = $formdata['about-investor'];
// Getting the WP_User object
$user = wp_get_current_user();
$userdata = array(
'about' => $about
);
if( isset($about) ){
update_post_meta($current_user, 'about', $about);
}
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'update_investor_profile', 1);
答案 0 :(得分:0)
这不是一件容易的事。 Contact Form 7插件无法访问表单字段的构造,因此,在表单加载时预填充字段的唯一方法是通过javascript。有一个Post My CF7 Form插件可以做到这一点,但并非旨在处理每个用户的个人资料。它具有内置的机制,可让您使用钩子以编程方式处理表单及其值的提交和加载,但又不是一个简单的答案。在这里,我将为您提供实现此目标所需采取的步骤的概述,
使用上述插件,您需要挂钩过滤器,
'cf7_2_post_save-post'将您的表单映射到您的个人资料。 您将需要在虚拟帖子上使用映射功能,为此使用帖子类型“ post”,以触发挂钩,
add_action('cf7_2_post_save-post', 'save_your_profile', 10, 3);
function save_your_profile($sky_key, $submitted_data, $submitted_files){
//check this is your form, using the unique key.
if('profile-form'!= $cf7_key) return;
//load your profile values
$submitted_data['field-name'];
}
“ cf7_2_post_form_values” ,以在表单加载时预填充字段
add_filter('cf7_2_post_form_values', 'load_profile_values', 10, 4);
function load_profile_values($values, $form_id, $post_type, $cf7_key){
//check this is your form, using the unique key.
if('profile-form'!= $cf7_key) return $values;
//load your profile values
$values['field-name']=<field_value>;
return $values;
}
替代自定义解决方案
由于您已经具有创建/保存新配置文件的有效解决方案,因此可以在页面加载后创建自定义JavaScript来填充表单字段。使用“ do_shortcode_tag”钩子来检测何时加载表单,
add_filter('do_shortcode_tag', 'append_script_to_form',10,3);
function append_script_to_form($output, $tag, $attr){
if('contact-form-7' != $tag){
return $output;
}
if(!isset($attr['id']) || 2 != $attr['id']){ //check for your form.
return $output;
}
//check if the user is logged in and has a profile, else return $output.
//load the profile.
$script = '<script>'.PHP_EOL;
$script .= '(function($)( $(document).ready(function(){'.PHP_EOL;
//each field is wrapped in a span.field-name element.
$script .= '$("span.wpcf7-form-control-wrap.field-name input").val("'.$profile_values["field-name"].'");'.PHP_EOL;
//... and so on
$script .= '});})(jQuery)'.PHP_EOL;
$script .= '</script>'.PHP_EOL;
//finally tag your script at the end of the form output.
return $output . $script;
}