Skip Navigation

[Resolved] Create edit form which combines user fields and several custom post fields

This support ticket is created 2 years, 8 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Author
Posts
#2134257

Tell us what you are trying to do?
- I am looking for the best way to create an edit form that combines user fields and several custom post fields.
Basically, it would be a CRED form that would populate information in form fields related to the logged-in user. I guess I would have to use generic fields and cred_save_data action but I am not sure how to do that.
I would appreciate your advice about how it could be achieved.

Is there any documentation that you are following?
- couldn't find anything particularly related to my question.

Is there a similar example that we can see?
- I don't have any

What is the link to your site?
- hidden link

#2134595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Could you give some more details about what you aim to achieve?

A single form can only publish (or edit) a Post, or register (or edit) a User.

It should be possible to add some user fields to a post form that you then use in some way after the form has been submitted using the cred_save_data hook, or possibly add some post fields to a user form and handle them in a similar way, which is it that you are trying to do?

If you can give some specific details it will be easier to advise you.

#2134751

Hi Nigel. Thank you for a quick reply.
I will try to explain the situation in more detail.
I have a membership site. Members (custom post type) can be part of the Organization (custom post type). Organizations to Members posts have a one-to-many relationship.

Members should be able to register on the site and in one registration form, where they create WP User account, Member post, and Organization post (or pick an organization if it was created before by another member).
After registration members also should be able to edit fields related to custom post types Members and Organization, as well as fields of WordPress User in one form.

#2134759

Regarding the "all-in-one" edit form...
I was thinking maybe to create a Member post edit form and somehow to pull data from related Organization post and related WP User account. That's where I need your advice on how to do that

#2135167

Okay it sounds like a lot you need to accomplish here. I can give you some examples to get started. I suggest you work on the first form to get started, and I suggest you start with a Create User Form. Why? Because if the first Form Creates a User profile, then you can use that User's ID as the post author of any other posts that are created during that first form submission. Otherwise, you may need to update the post author for each post later. So first, create a New User Form. That form will include all kinds of fields automatically for setting the new User's login, password, email, any custom User fields, and so on. You'll also add in generic fields to capture information about the Member post you want to create, the Organization post if you want to create one, or the selected Organization if your User chooses an existing one.

To create a generic field that contains all the existing Organizations as options, create a View of Organizations using the legacy View editor. Run the Loop Wizard and choose the "list with separators" loop style. In the last screen of the wizard popup, insert the post title and click finish to create the loop automatically. Then replace the contents of the wpv-loop tags as shown here:

<wpv-loop>
      [wpv-item index=1]
        {"value":"1","label":"Create an Organization"},{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

You can now insert this View as the options for a generic select field in your Form. I would switch to expert mode. Generic select fields populated by Views have this general syntax in the expert builder:

[cred_generic_field field="generic-organization-select" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"",
"options":[ [wpv-view name="your-organizations-view-slug"] ] 
}
[/cred_generic_field]

Once you have your Form populated with all the necessary generic fields, you'll start working on the PHP code that captures those generic field values and uses them to create new posts and add post relationship associations. Use the cred_save_data API for this purpose. This code can go in your child theme's functions.php file, or in a new snippet set to run everywhere and activated in Toolset > Settings > Custom Code. Here's a template you can use to get started:

// one form to create user, member, and possibly organization
// https://toolset.com/forums/topic/create-edit-form-which-combines-user-fields-and-several-custom-post-fields/
add_action('cred_save_data', 'create_user_automation',10,2);
function create_user_automation($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==123)
    {
        // get a generic field value from the $_POST superglobal under the slug key
        $selected_organization = $_POST['generic-organization-select'];
        $member_title = $_POST['generic-member-title'];
        $member_field_one = $_POST['generic-member-field-one'];
        // ...add additional field values here as needed


        // insert the new member post
        $member_args = array(
          'post_title'    => $member_title,
          'post_type'    => 'member',
          'post_status'   => 'publish',
          'post_author'   => $user_id
        );
        $member_id = wp_insert_post($member_args);

        // set a custom field in the new member post
        // remember that Types fields use a slug prefix of wpcf- in the database
        update_post_meta($member_id, 'wpcf-member-field-one-slug', $member_field_one);

        if($selected_organization==1){
          // in this case, the User did not select an existing organization 
          // so you probably need to create one like you just created member with wp_insert_post
          // then connect it with toolset_connect_posts like the example in the else block below
          // your code to insert the organization post should go here

        } else {
          // in this case the User selected an existing organization
          // connect new member and selected organization in the post relationship with slug 'member-org-rel-slug'
          // adjust that slug if needed
          toolset_connect_posts( 'member-org-rel-slug', $member_id, $selected_organization );
        }
    }
}

Replace 123 with the numeric ID of the Create User Form, and replace the various field and relationship slugs throughout the code as needed. You can see several examples in the code, including how to access the value of a generic field, how to create a post, how to add a custom field to a post after you create it programmatically, how to test the value of a generic field and set up a conditional based on that value, and how to connect two posts in a post relationship programmatically. ​

Documentation for the major PHP APIs used in this code:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://developer.wordpress.org/reference/functions/update_post_meta/

I think this gives you enough to get started on setting up that first Create User Form, including generic fields, maybe including a select field for existing Organizations, and then setting up your cred_save_data hook for the first Form.

Once that's working as expected, I think the next step is to create an edit User form and add all the same generic fields. The trick will be how to show all the correct information in those fields, and I think you'll need a custom shortcode for that. I'm getting ahead though, let's get form 1 and snippet 1 working correctly before moving on to editing.

#2135939

thank you very much, Christian! This is a huge help. I will work on the 1st form

#2137027

Sounds good, I will stand by for your updates.

#2139369
OrganizzationGenericField.jpg
OrganizationView.jpg

Hi Christian. I got an issue with the form 🙁
I created an organization view which I inserted as a shortcode into the select generic field as per your advice.
However, the select field doesn't show up on the form.
When I was creating the select generic field for Organization you chose an option to get results from the shortcode. I also checked that the view works well and gives results in the JSON. The generic field works ok as well when pure JSON is used. But it doesn't work with the shortcode for some reason.
I attached screenshots of the view and the Organization generic field just in case

#2139569

... additionally, I was also trying to use curly brackets instead of square brackets for the Organization view shortcode.
I also was trying to use a filter to clear the view (https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms/) but no luck.

#2139699

Looks good to me, is it possible for me to see the output of the View placed on your site somewhere (outside of the generic select field options, I mean)? I'll be able to get a better idea of what might be happening in the results.

#2140303

Yes, sure. Here is a link to the page with the Organization view output: hidden link

#2140469
Screen Shot 2021-08-11 at 2.19.25 PM.png

The syntax looks accurate, but there are extra line breaks and spaces in the code produced by the View. See the screenshot here, if you examine the code in the browser inspector you can see the output markup has extra spaces and line breaks that are not obvious in the rendered results. These extra spaces and line breaks in the output will throw off the generic field options interpreter.

First, I would try to resolve this by running the Loop Wizard again. Edit the View, select all the code in the Loop Editor panel and copy it. Then click "Loop Wizard" again and be sure to choose the List with separators list format, because this applies a filter that should help strip out extra spaces and line breaks. In the final screen of the popup, if there are any fields in the loop builder, delete them. Add the post ID field (default options for the field are fine) and finish the wizard. Back in the Loop Editor panel, delete everything from the panel and paste the code you previously copied. Be sure there are no extra lines or extra spaces after the wpv-layout-end shortcode. This will hopefully prevent linebreaks and spaces from breaking the results again. If not, I have a couple of other ways to filter the output if necessary.

#2140651
Output_with_quotes.jpg
View.jpg

Thank you, Christian.
I did what you recommended. As I see the output doesn't have any gaps anymore.
I even tried to create a new view from scratch but the results remain the same. You can see my Organization view in the attachment.

Also, I noticed that the view result which you can see by the link: hidden link has quotes around.
I was trying to use 'wpv_filter_wpv_view_shortcode_output' filter to remove them but it didn't help. Maybe somethings adds quotes after the filter takes effect? Not sure if this could be an issue at all. (please see the output with quotes on the second screenshot attached)

#2141161

Maybe somethings adds quotes after the filter takes effect? Not sure if this could be an issue at all.
No that's just a side effect of the browser inspector. Something else must be going on. May I take a look in wp-admin? Please let me know where the Form can be found on the front-end of the site.

Private reply fields are active here so you can share login credentials securely.

#2141479

Thanks, I'm a bit stumped and I've tried several tests without much luck. I'll continue to investigate and give you another update as soon as I can.

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