[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.
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?
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.
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.
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:
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.
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 );
}
}
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?
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.
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?
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.
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 🙂
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.