Skip Navigation

[Resolved] Auto create custom post after new user submission

This support ticket is created 7 years, 2 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)

This topic contains 18 replies, has 3 voices.

Last updated by virageguitars 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#490658

Hi, evebody,

We have:
- CRED user registration form 'Add user'
- in this form we have the custom field 'title'
- custom post 'organizer'.

I'd like:
when a new user is registered I need to create in auto mode (!) new custom post 'organizer' with post-title=custom field 'title'

Is it possible?

Thanks to all for help

#490669

Hi there, I think I understand. You would like to automatically create a new post of type "Organizer", including the post title field, whenever a user is created using your CRED form. Correct?

This functionality isn't available in the wp-admin area, but it can be accomplished with the CRED API in PHP if you are comfortable editing code. You can use the 'cred_save_data' hook to perform some action (like creating a new post) when your form is submitted and the information is being saved to the database. Here is the documentation for that API hook:
https://toolset.com/documentation/user-guides/cred-api/#csd

You can create a post programmatically using WordPress function wp_insert_post. Here's the documentation for that:
https://developer.wordpress.org/reference/functions/wp_insert_post/

Please let me know if I can provide additional guidance on this, and I'll be happy to take a look.

#491113

Hi, Christian,

Yes, uou understand correct.

Just in case, the worlflow is:
1) User fill the CRED user registration form 'Add user' (the form has field 'organizer')
2) User save the form
3) "Save new user" process should work as a trigger - to create new custom post 'organizer' and populate field 'organizer' (in 'Add user' form) to title field in this custom post 'organizer'

Do you can you help me to construct such hook?
I would be grateful if you could help me in the matter 🙂

Best,
Henadz

#492026

Hi Henadz,

Okay, I think we are understanding each other correctly. As I mentioned, the PHP API is for users who are comfortable writing their own custom PHP code. Writing custom code for you is outside the scope of the support we provide here, but I can provide you with some guidance about how to do this yourself. The links I provided include these examples that are relevant:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // change this 12 to the actual id of your form
    if ($form_data['id']==12)
    {
        //this is where you will insert your post as described below
    }
}
// Create post object
// You may need to modify the post author and post categories for your needs
$my_post = array(
  'post_title'    => $_POST['post_title'],
  'post_content'  => $_POST['post_content'],
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 8,39 )
);
 
// Insert the post into the database
wp_insert_post( $my_post );

Have you written any code that I can see and help troubleshoot?

#492228

Hi, Christian,

Thank you for your step outside the scope of the support you provide. I very much appreciate your kind words.
But I am not familiar so good with PHP. And I read this links. But this is not moved me to my goal.
One workable sample that's all I needed 🙂

My issue is:
User registration process (user CRED form) automatically create (when user form is saved) a custom post "organizer" and populates the title of this custom post by value of custom field "organizer" wich user fill in registration form.
This is possible?

Can you help me to construct such hook?

#492414

Hi, the two examples I provided can be used to do exactly what you're describing with a bit of adjustment. First, you need to add this code to your theme's functions.php file:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // change this 12 to the actual id of your form
    if ($form_data['id']==12)
    {
        //this is where you will insert your post as described below
    }
}

You must change the 12 to the ID of your user CRED form. You can find that ID by going to Toolset > User Forms in the wp-admin area. Next to each form you will find an ID. Change 12 to that number. Then beneath the line that begins "//this is" you will add the code shown below.

// Create post object
$my_post = array(
  'post_title'    => $POST['wpcf_your-organizer-field-slug'],
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_type' => 'organizer'
);
  
// Insert the post into the database
wp_insert_post( $my_post );

Here you must change 'your-organizer-field-slug' to match your organizer field slug. Keep the 'wpcf_' part, because this is required. Change the post_author to use the correct author ID. Change the 'organizer' slug to match your organizer post type slug if necessary.

Save this functions.php file on your server, then use your CRED form to create a user. Check the organizers list to see if your post was created successfully.

If this does not work for you, please copy and paste the code you have added to functions.php and I'll take a look.

#493092
user_reg_create_org_v1.jpg

Not works 🙁
This is code that I added.

/** Creaet new Organizer when user registered */
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==149)
    {
	$my_post = array(
	'post_title'    => $POST['wpcf-main-organizer'],
	'post_content'  => '',
	'post_status'   => 'publish',
	'post_author'   => $user_ID,
	'post_type' => 'organizer'
);
   
wp_insert_post( $my_post );
	}
}

'wpcf-main-organizer' - the slaug of user custom field, that must be inserted to title of CPT 'organizer'

In attachment see result - title is empty 🙁

(I am using to approve new users plugin New User Approve)

#493232

Hi, check your PHP syntax:

$POST['slug']

should be

$_POST['slug']
#494399
auto_create_org_1.jpg

Hi, Christian,

Yes, it's works!
But 🙂
A newly created CPT 'organizer' should have the author of a current user that was registered.
Is it possible?

Thanks a lot

#494695

Sure, it's possible. There is a function in WordPress that will get the current user's ID for you:

https://developer.wordpress.org/reference/functions/get_current_user_id/

Call that function instead of $user_id.

#494705

I'd like to save info about author (new registered user) when CPT 'organizer' is auto creating by current user.
Now, 'organizer' does not know anything about who created it, see image.
Is it possible?
Can you add to your code the necessary line?
Thanks

#494711

You want the id of the user that was just created? That is represented by the variable $post_id as you can see here:

function my_save_data_action($post_id, $form_data)

So instead of $user_id or get_current_user_id, you can just use $post_id.

#494724

Ok, maybe I don't understand right.
Please, see image - I can not see any information about which user creates the 'organizer'

#494730

In this case the post id variable represents the ID of the user that was just created. The function "my_save_data_action" will be called after the new user is saved. When the function is called, it receives the information about the user that was just created.

So you can use $post_id to get the ID of the user that was just created.

#494750

Sorry, but I don't understand

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.