Skip Navigation

[Resolved] Create new post (and post title) when new user created by import

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by JamesS2731 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1585527

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

#1585787

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));
...
#1585917

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

#1585921

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;
#1585925

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

#1585991

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