Skip Navigation

[Resolved] Create a user and his posts at the same time

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Beda 5 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1226493

Hello,

I have a two custom post types : Students and Courses.
I have two "many to many" relatiships between these two cpt: Favorites and Subsriptions.
A WordPress user can only have one Student post (I don't know how to do it right now, but I'll think about it later).

When a user is in a Course page (single page created with Elementor), he can bookmark it or subscribe to the course.

I've read some answers to my next question, but I steel need help, with step-by-step guidance and examples of functional codes, please (of course, if you can)!

I need when the user presses the "add to favorites" button:

- create an account (I created the user form to do it)
- create an associated Student post, containing his first name, his name and his email address from his account informations
- create the relationship (Favorites) between the Student cpt and the Course cpt

And for the connected user, with already an account:

- create, if it doesn't exist, the corresponding Student post, containing his first name, last name and e-mail address
- create the relationship (Favorites) between the Student cpt and the Course cpt

Can you please help me with that?

If I think badly, let me know, please.

Best regards,
Mansario

#1226650

1. A WordPress user can only have one Student post (I don't know how to do it right now, but I'll think about it later).

There is a simple trick elaborated here about this:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/#how-do-i-prevent-users-from-creating-more-than-one-contractor-post

That post will also help you further with the connections of posts/users

2. I need when the user presses the "add to favorites" button:
- create an account (I created the user form to do it)
- create an associated Student post, containing his first name, his name and his email address from his account information
- create the relationship (Favorites) between the Student cpt and the Course cpt

I assume that Button is yet just an Elementor Button, right?

Hence, a prototype of the idea you outline, and not a Relationships form or similar.

In that case, I suggest:
- since you want the button to create an account, the "user" pressing the button is not yet a user, but a "guest". In WordPress, users are logged in, existing users, while guests are visitors without any role on the site.
To create an account in Toolset you use Toolset User Forms. That Button there could maximally lead to a Form that lets the guest fill in some details (user profile) and submit it. Of course, you could insert the Form on that very page and then use the Forms submit button to have the "button" to click.
User forms are explained here, they are quite straight forward to set up:
https://toolset.com/documentation/user-guides/front-end-forms/
You create one in Toolset > User Forms.
- the same button (in this case the User Form submit button) cannot natively create a post as well. That requires Custom Code.
You'd use the Toolset Forms API to apply some WordPress API to the cred_save_data() action. This is fired when the form saves the post (or user) to the database. In that action you can use wp_insert_post() to create a new post with data that for example, you grab from the $_POST of the User Form:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_insert_post/
Example code, to be adjusted:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)//Change the ID of your form
    {
       wp_insert_post( array $postarr, bool $wp_error = false )//More details about wp_insert_post() here: https://developer.wordpress.org/reference/functions/wp_insert_post/
    }
}

- to create the relationship of that post (I suspect of type "Student") to the "Course" type, you will then (in the same cred_save_data() but after wp_insert_post()), use the Toolset relationships API to insert the relationship.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Example code to be adapated, an enriched version of above:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)//Change the ID of your form
    {
       $my_post = wp_insert_post( array $postarr, bool $wp_error = false )//More details about wp_insert_post() here: https://developer.wordpress.org/reference/functions/wp_insert_post/
       toolset_connect_posts( 'Favorites', $my_post->ID, 7 );//where 7 the ID of the course, you could get that from the current post object or elsehow from a Form Field or similar, it depends where that data is stored. "Favorites" is the relationship slug
    }
}

It would probably, however, be much easier for you (if you are not comfortable with PHP custom code) to follow a few more steps (not all in the click of one button) and use the Relationships Toolset Forms to connect the posts.
https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

3. The same is valid for if the user exists already, however here we would ensure with an Access ShortCode or an HTML condition, that existing users do not see the "Create user" form, instead, just a button to connect (and create) posts.

Now, this is a rough overview of how it's done.
I cannot assist you in each sub detail here in the same thread.

Let's use this as a "how to" generic thread, where from you can create subsequent single threads when specific issues/questions come up.

I'd start with the HTML/Access conditions to show/not show data to existing or new users (guests)
Information on that can be found here:
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Once you tackle that down, attack the other points and eventually open new single topics for each point so we can help with precise information and more elaborated examples on the Custom Code.

Thanks!

#1226921

Hello and thank you very much for your answer!

I managed to get point 2 to work (for those who will use the code that is given as an example, think to modify $my_post-> ID by $my_post).

For point 3, I tried several combinations of views, forms, ..., I still can't make my "button". Can you guide me a little more please?

"My" idea is to make a view with the Students who have Favored the Course. Filter this view to keep only the Student that corresponds to the Current User. If it is not in this result, I display a button (I don't know yet what it is like button, or how to do this view) that will execute a php code.

Thanks for your help!
Mansario

#1227737

A button per se cannot do all this.
Either, the button is the very submit button of a Toolset Form, or, it's a button leading to a page with such a form.
Executing PHP thru the click of a button is something that Toolset does not offer.
You could, of course, apply some script that calls a php function on click of a button: https://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button

But I think you mean the act to create a new user or post, and that (in Toolset) is done with the forms, so you will either include the Form in the View or a button that leads to another page where that form resides.

Assuming a view would display that button only if the user has no posts, then the button (form or button to another page) would live inside the "no-items-found" section of the Views loop since that is what Views display when no results are found.
This is also elaborated here:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/#how-do-i-prevent-users-from-creating-more-than-one-contractor-post