Skip Navigation

[Résolu] Automatically create a custom post during User registration with CRED and restrict access

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a new User CRED form that allows guests to register. When they submit the form, I would like to automatically create a custom post that includes information from the form, and I would like to restrict access to that post to the created member only.

Solution:
1. Create a Custom Post Entry for "Band Name" on Member Registration.
This can be accomplished with a bit of custom code using the cred_save_data API.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==1234)
    {
      $my_post = array(
        'post_title'    => $_POST['some_custom_input'],
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'band-post-type-slug'
      );
         
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

1234 should be changed to match the numeric ID of your User CRED form, and 'some_custom_input' should be changed to match the name of an input containing the data you would like to use in the Band post title. The data in the $my_post array can be modified according to the wp_insert_post documentation.

2. Only allow the registered Member edit his own Band Page.
In the code I provided above, I have configured the new post's author to be automatically set to the User that was just created during registration. Once this registration form is up and running, you should be able to use Access post and form restrictions based on the post Author (Member can edit own vs Member can edit any).

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/setting-access-control/
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/

100% of people find this useful.

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.

Auteur
Publications
#581591

Tell us what you are trying to do?

I am trying to expand a band Directory website to show lots of info about each band.
Currently Bands are Members of the Association / Website.

I had built the views and content templates fine, but hit a bump trying to do 2 things.

1. Create a Custom Post Entry for "Band Name" on Member Registration.

2. Only allow the registered Member edit his own Band Page.

Having gone a different direction, I created the user role (Band Admin)

I added the necessary fields to the User Profile like Band Biog, Band Image, Videos etc instead.

But it's not the ideal way to go for the end result as i wanted to have.

Ideally, I wanted to have the band admin signup form automatically create a new Band Page (CPT).
So Bob Murphy Registers Himself and his band name "The 123's" via a Cred Form.

He is associated with a Custom Post Entry in the Band Pages.

Band Pages > The 123's

I had created a page (containing a cred form) to edit the info but also need help preventing him from seeing other entries.

if I can't auto create a Custom Post Type Entry on signup, I don't mind doing it manually. But I do need to lock up editing the content to the user exclusively.

This way I can still use use Child Posts as Testimonials, Reviews & Playlists.

Is there any documentation that you are following?
Google!

Is there a similar example that we can see?

What is the link to your site?

I'm between two different plans at the moment,
Somewhere between Band profiles being CPT's and Band Info being User Fields.

???

Can I either - Create a custom post type entry on signup?
or - Lock A Custom post type to the associated author somehow.

please and thanks

This is close to what Im trying to do.
hidden link

#581766

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

1. Create a Custom Post Entry for "Band Name" on Member Registration.
This can be accomplished with a bit of custom code using the cred_save_data API. I have included a sample below, but yours may need some modification based on the data you want to collect from the User form and apply to the Band post. I can help you figure this out if you can give me more details about the data collected by your form and how you want to use that in the automatically generated Band post.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==1234)
    {
      $my_post = array(
        'post_title'    => $_POST['some_custom_input'],
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'band-post-type-slug'
      );
        
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

1234 should be changed to match the numeric ID of your User CRED form, and 'some_custom_input' should be changed to match the name of an input containing the data you would like to use in the Band post title. The data in the $my_post array can be modified according to the wp_insert_post documentation: https://developer.wordpress.org/reference/functions/wp_insert_post/

More info about our cred_save_data API here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

2. Only allow the registered Member edit his own Band Page.
In the code I provided above, I have configured the new post's author to be automatically set to the User that was just created during registration. Once this registration form is up and running, you should be able to use Access post and form restrictions based on the post Author (Member can edit own vs Member can edit any).

More information about Access controls:
https://toolset.com/documentation/user-guides/setting-access-control/
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/

Let me know if these solutions aren't working as expected and I can take another look.

#584156

Just wanted to follow up and say Thank you so much for the help.
It was the cornerstone of the project really, and now we are moving along nicely.

always a pleasure to work with this toolset.

Thanks again,

David

#584247

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

Great! Good luck with your band directory project.

#592445
Edit Member Profile ‹ Wedding Band Association — WordPress (1).png
Edit Testimonial ‹ Wedding Band Association — WordPress.png

Hi Christian.

Can you give me some more direction in terms of the above, please?

The situation is this:

This is a Directory of Wedding Bands. (CPT = wedding-bands)
Bands will have a Biog, Band Logo, videos, all as post fields of the CPT.

Then we have Testimonials.
These are a child post type of 'wedding-bands"

Title, Content, Name of the person leaving the review and 5-star rating.
These can only be entered by the band admin. Not the public.

So,
Band Admins register as a custom User Role (Band Admin)
During the registration via the Cred Form, we worked with previously (above), You helped me create a Custom Post Type for the Band.
So the user is registered and the band Name FIeld in the Form is to create a new CPT in the wedding bands CPT.

Now, the problem I am having is with Testimonials.
I want the Cred Form to Add a Testimonial and associate it with the band.

I am using a page for the admin to add/edit testimonials called
hidden link

It contains a view called "testimonials-back-end"

In that View, I filter Testimonials like this "Select posts with the author the same as the current logged in user."

It shows testimonials and offers a link to a Layout to edit existing ones,
and a form to add new ones.
[cred_form form="testimonials"]

I am trying to get this form to tie new testimonials to the Wedding band.
I don't want a drop down for members to select their band.

Is there a way I can get the Cred Form to know what band is relevant based on the User who is logged in.
I would like the testimonials to show - This Testimonial Belongs to Member Profile { the author's band }

I hope the screenshots make this easier to understand.

#594780

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

Hi, I have been away for a few days but I see that you opened another ticket:
https://toolset.com/forums/topic/cred-form-associating-with-cpt/

Let me know if you still need assistance here.