Tell us what you are trying to do? I'm trying to create a new post when a new user is created by import and use the users first and last name to create the post title.
Is there any documentation that you are following? no
Is there a similar example that we can see? yes, but on another site.
What is the link to your site? hidden link
Hi there,
I'm trying to create a new post when a new user is created by import (WP All Import) and use the users first and last name to create the post title.
I've got this workmen when I manually create a new user, but when I import a new user, the post title of the post is left blank.
I'm using the following code:
/**
* Create Member post when a new user is created and make the new user the author of it.
*/
add_action( 'user_register', 'add_member' );
function add_member( $user_id ) {
$new_member = array(
'post_title' => wp_strip_all_tags( $_POST['first_name'] . " " . $_POST['last_name'] ),
'post_author' => $user_id,
'post_type' => 'member',
'post_status' => 'publish',
'meta_input' => array(
'member_first_name' => $_POST['first_name'],
'member_last_name' => $_POST['last_name']
),
);
wp_insert_post( $new_member );
}
Any thoughts or ideas most welcomed.
Best regards
James
Hi, what happens if you try hard-coding the post title? Test this:
...
function add_member( $user_id ) {
$new_member = array(
'post_title' => 'Test Post',
...
What about the meta values for member_first_name and member_last_name - are those set correctly in postmeta? FYI Types fields use a wpcf- prefix in the database, so if you're trying to set the value of fields created in Types, you should add that prefix to both field slugs:
...
'wpcf-member_first_name' => $_POST['first_name'],
'wpcf-member_last_name' => $_POST['last_name']
...
If the meta values are set correctly, then something else may be changing the post title after this code is run. Or, the first and last names in $_POST could be wrapped in HTML tags - wp_strip_all_tags will not only strip the tags, but also all the content in the tags: https://developer.wordpress.org/reference/functions/wp_strip_all_tags/
If nothing seems to be working correctly, log or var_dump the $_POST object to see what's available. Perhaps the first_name and last_name keys are wrong.
...
function add_member( $user_id ) {
error_log(print_r($_POST, true));
...
Hi Christian,
Many thanks for your quick response.
I tried hard coding the post title and that did indeed work.
I've also tried removing wp_strip_all_tags but that didn't help. The fields to populate the post title are just the standard WP user first and last name fields.
The members first and last names were just extra fields in the post type, but they're not necessarily required (incidentally, adding the wpcf- didnt help populate them).
I'll add the error log line to see what output we get.
In the meantime, is there anything else I can try?
Kind regards
James
The first and last name probably aren't in $_POST - that would only work if a form of some kind had been submitted including fields with those names. Instead, you probably have to use the $user_id parameter made available in your callback to get the first and last name from the database with get_userdata:
$info = get_userdata( $user_id );
// First name: $info->first_name;
// Last name: $info->last_name;
Ah, yes. I did think that, but wasn't sure how to call the user data.
I've inserted that as shown below, but I'm still getting (no title) as the post title. Did I add it in the correct place?
add_action( 'user_register', 'add_member' );
function add_member( $user_id ) {
$info = get_userdata( $user_id );
// First name: $info->first_name;
// Last name: $info->last_name;
$new_member = array(
'post_title' => ( $_POST['first_name'] . " " . $_POST['last_name'] ),
'post_author' => $user_id,
'post_type' => 'member',
'post_status' => 'publish',
'meta_input' => array(
'member_first_name' => $_POST['first_name'],
'member_last_name' => $_POST['last_name']
),
);
wp_insert_post( $new_member );
}
J
Ok, fixed it. I used that code as follows and it all works:
/**
* Create Member post when a new user is created and make the new user the author of it.
*/
add_action( 'user_register', 'add_member' );
function add_member( $user_id ) {
$info = get_userdata( $user_id );
// First name: $info->first_name;
// Last name: $info->last_name;
$new_member = array(
'post_title' => ( $info->first_name . " " . $info->last_name ),
'post_author' => $user_id,
'post_type' => 'member',
'post_status' => 'publish',
'meta_input' => array(
'member_first_name' => $_POST['first_name'],
'member_last_name' => $_POST['last_name']
),
);
wp_insert_post( $new_member );
}
Many thanks for all of your help!
Kind regards
James