提交表单后更改 post 的作者 - Post 我的 CF7 表单
Change author of the post after form sumbit - Post my CF7 Form
我使用插件“Post my CF7 Form'
我有一个带有“name”的表格并联系“phone”,在 sumbit 插件制作一个标题为“name”
的 post
我有这段代码可以创建用户名为“phone”字段的用户:
function create_user_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() == 'Форма заказа') {
$password = $formdata['phone'];
$email = $formdata['phone'].'@thechip.pro';
$name = $formdata['phone'];
// Construct a username from the user's name
$username = strtolower(str_replace(' ', '', $name));
$name_parts = explode(' ',$name);
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' => $username,
'user_pass' => $password,
'user_email' => $email,
'nickname' => reset($name_parts),
'display_name' => $name,
'first_name' => reset($name_parts),
'last_name' => end($name_parts),
'role' => 'author'
);
$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 .= wp_login_url() . "\r\n";
wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
请帮帮我,我怎样才能让我的新用户成为创建者 post?
我正在尝试使用此代码
add_action('cf7_2_post_form_submitted_to_profile', 'new_profile_mapped',10,4);
/**
* Function to take further action once form has been submitted and saved as a post. Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
* @param string $post_id new post ID to which submission was saved.
* @param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
* @param string $cf7form_key unique key to identify your form.
* @param array $submitted_files array of files submitted in the form, if any file fields are present.
*/
function new_profile_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
//do something.
$userdata = array(
'user_login' => $cf7_form_data['phone']
);
$user = get_userdatabylogin($userdata);
if($user){
$arg = array(
'ID' => $post_id,
'post_author' => $user->ID,
);
wp_update_post( $arg );
}
}
但是它不起作用,有人可以帮助我吗?
看来你在这方面有问题 get_userdatabylogin($userdata);
尝试用这个替换
function new_profile_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
//do something.
$user = get_user_by('email', $cf7_form_data['phone'].'@thechip.pro');
if($user){
$arg = array(
'ID' => $post_id,
'post_author' => $user->ID,
);
wp_update_post( $arg );
}
}
我使用插件“Post my CF7 Form'
我有一个带有“name”的表格并联系“phone”,在 sumbit 插件制作一个标题为“name”
的 post我有这段代码可以创建用户名为“phone”字段的用户:
function create_user_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() == 'Форма заказа') {
$password = $formdata['phone'];
$email = $formdata['phone'].'@thechip.pro';
$name = $formdata['phone'];
// Construct a username from the user's name
$username = strtolower(str_replace(' ', '', $name));
$name_parts = explode(' ',$name);
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' => $username,
'user_pass' => $password,
'user_email' => $email,
'nickname' => reset($name_parts),
'display_name' => $name,
'first_name' => reset($name_parts),
'last_name' => end($name_parts),
'role' => 'author'
);
$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 .= wp_login_url() . "\r\n";
wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
请帮帮我,我怎样才能让我的新用户成为创建者 post?
我正在尝试使用此代码
add_action('cf7_2_post_form_submitted_to_profile', 'new_profile_mapped',10,4);
/**
* Function to take further action once form has been submitted and saved as a post. Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
* @param string $post_id new post ID to which submission was saved.
* @param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
* @param string $cf7form_key unique key to identify your form.
* @param array $submitted_files array of files submitted in the form, if any file fields are present.
*/
function new_profile_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
//do something.
$userdata = array(
'user_login' => $cf7_form_data['phone']
);
$user = get_userdatabylogin($userdata);
if($user){
$arg = array(
'ID' => $post_id,
'post_author' => $user->ID,
);
wp_update_post( $arg );
}
}
但是它不起作用,有人可以帮助我吗?
看来你在这方面有问题 get_userdatabylogin($userdata);
尝试用这个替换
function new_profile_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
//do something.
$user = get_user_by('email', $cf7_form_data['phone'].'@thechip.pro');
if($user){
$arg = array(
'ID' => $post_id,
'post_author' => $user->ID,
);
wp_update_post( $arg );
}
}