Skip Navigation

[Resolved] Automatic post creation on registration

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to create post automatically when user register.

Solution:
To create a new post when user register you need to use CRED hook cred_save_data with WordPress native function wp_insert_post to insert the new post.

You can find the proposed solution with the reply at here and here.

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/cred-api/#csd
=> https://developer.wordpress.org/reference/functions/wp_insert_post/

This support ticket is created 7 years, 3 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.

Our next available supporter will start replying to tickets in about 0.55 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by martinN-3 7 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#555095

I am trying to automatically create a post assigned to a user after that user registers using a form created using CRED, later I want to show that post on a page using a view. It kinda works but there are still some issues..

First I had created a user field group that would have a field "business name". I added that field to the registration form and a post with that name was created successfully on registration.

The issue with that is that when I create a view for the post type "business" and display it on the users account page, it doesn't find any posts for some reason. So I thought to rather make a post field group with various fields (adress, phone, email, etc..) but only include the "business name" field (now in the post field group instead of a user field group). I added it to the form but now it just doesn't display on the frontend inside the form.
Is that because only user fields can be shown or am I missing something?

The ultimate goal on this is the following:

1. User registers and gives a name for his business
2. A post with the title given by the user is created and assigned to him
3. From the my account page, all other related fields are shown and also a link to modify/add the remaining business information such as address, email, etc.

4. A child posttype of business "location" can be created by the user using a cred form. I got the form and all but it asks to select a parent "business" and even lets me select businesses from other users. Is there anyway to either autmatically assign the "business" post of that user (there will always only be one) or to at least only show him the choice of his own business?

#555122

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you can not display post fields on CRED user forms as both are the different entity. You can display post fields on CRED post forms and user fields on CRED user forms.

But if you want to capture all field information (for business + locations) then you can use CRED generic field to capture user input and then use these field values as input for your CPT (business + location) post fields.
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

Now, I guess you should have CPT business (parent) and child post type (location). You need to use CRED hook cred_save_data.

You should create user account using user form and add all other fields using generic field for which you need to take input from user so you will have all the field information available posted by CRED form with $_POST PHP global and you can use cred_save_data hook to capture the post values by CRED form and you should create post entries accordingly.

You can use CRED hook: cred_save_data
=> https://toolset.com/documentation/user-guides/cred-api/#csd

Now, using above hook you can create child entry using the WordPress function - wp_insert_post:
=> https://developer.wordpress.org/reference/functions/wp_insert_post/

You can see here how you can link parent and child using CRED hook: cred_save_data
=> https://toolset.com/forums/topic/automatically-create-child/#post-491114

#555175

Hi Minesh,

This is what I have got so far but its not working yet, I think maybe my error is in the name of the field or something please check the following code:

Generic Field:

[cred_generic_field field='Firmenname' type='textfield' class='fnamereg' urlparam='']
{
"required":1,
"validate_format":0,
"default":""
}
[/cred_generic_field]

functions.php:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==14)
    {
        // Create post object
$my_post = array(
  'post_title'    => $_POST['wpcf-firmenname'],
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_ID,
  'post_type' => 'firmen'
);
   
// Insert the post into the database
wp_insert_post( $my_post );
    }
}

About the childposts. I do not need to automatically create them, the user will do that using a front end form. I just don't them to be able to select a business of another user as the parent of their childpost.

Thanks 🙂

#555243

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ahh - Yes - error is in your code. When you use generic field you need to use exactly field name as you given to field. But if you use field created using Types (post fields) then you need to prefix it with 'wpcf-'.

So, having said that, you should try the following code which should work for you where I've corrected the post filed name for your generic field.:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==14)
    {
        // Create post object
$my_post = array(
  'post_title'    => $_POST['Firmenname'],
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_ID,
  'post_type' => 'firmen'
);
    
// Insert the post into the database
wp_insert_post( $my_post );
    }
}
#555247

That worked thanks! 🙂

Now there is just one more issue..

So the business is created but in the view that is supposed to show it says "No items found
". I have the view set to "Select posts with the author the same as the current logged in user." so I suspect the business is not being correctly assigned to the user that filled out the form/registered. How can I go about this to make it work?

#555249

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I need login details to check whats going on your install.

Could you please share problem URL where you added the view.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#555260

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I do not want the registration URL but which user you registered with and which post you created and where you are displaying the view to display posts of the current logged-in user?

Could you please share URL of above post created and page URL where you added the view to display loggedin user posts?

#555275

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I checked on your install and I found that the post author is different for the following post:
=> hidden link

Also, I found a little typo/mistake in your code where the variable is $post_id not $post_ID as well as post_type should be assigned with post type slug. I've corrected those things with the following code: so please try to use the following code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==14)
    {
        // Create post object
$my_post = array(
  'post_title'    => $_POST['Firmenname'],
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_id,
  'post_type' => 'firma'
);
     
// Insert the post into the database
wp_insert_post( $my_post );
    }
}

Now, logout from your browser, try to create a user with "Firmenname" and login using the created user in different browsers and check if you can see the post under profile page.

#555278
Screen Shot 2017-08-01 at 17.46.53.png

Awesome that worked perfectly thanks a lot 🙂

Now the only thing remaining is (if even possible) to dont let users select other business when creating a child post but rather automatically assign it to the business created by the user on registration. See what I mean in the attached screenshot how the user can select others users business as parent. Thanks 🙂

#555285

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that solution I provided works really well. As your original issue is resolved, could you please kindly mark this ticket resolved.

As per our support policy, we entertain only one question per ticket. Could you please kindly open a new ticket for your each new question. This will help other users searching on the forum. Thank you for understanding. Even you can assign the new ticket to me but I will handle that tomorrow.

Have a great day. 🙂

#555297

Of course, thanks a lot for everything 🙂