Skip Navigation

[Resolved] Dynamic checkboxes field to associate Users with custom posts created in CRED

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

Problem:
Create a View of Users filtered by role, and use that View to populate the options for a generic field in your CRED form.
Solution:
It requires custom codes, for example, you can try these:
1) Create a view "my-user-view" to list users, output the results as JSON format:

...
    <wpv-loop>
      [wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
      [wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
    </wpv-loop>
...

2) Create a CRED form with a generic field, use above view as the options of this generic field, like this:

[cred_generic_field field='my-generic-field' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view name="my-user-view"] ]
}
[/cred_generic_field] 

3) when user submit the CRED form, use action hook "cred_save_data" to save the value into database

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 1 month 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#578841

Branching out from the thread at https://toolset.com/forums/topic/creating-functions-for-inventory-management/page/2/#post-578835 to create the process below...

Creating a View of Users filtered by role, and using that View to populate the options for a generic field in your CRED form. Then you can capture the generic field values selected by the Owner and use those values to set the value of your actual custom field in the backend.

#578921

Dear Jon,

It requires custom codes, for example, you can try these:
1) Create a view "my-user-view" to list users, output the results as JSON format:

...
    <wpv-loop>
      [wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
      [wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
    </wpv-loop>
...

2) Create a CRED form with a generic field, use above view as the options of this generic field, like this:

[cred_generic_field field='my-generic-field' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view name="my-user-view"] ]
}
[/cred_generic_field] 

3) when user submit the CRED form, use action hook "cred_save_data" to save the value into database:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

See similar thread:
https://toolset.com/forums/topic/allow-editor-to-select-appropriate-author-on-cred-edit-content-form/

#579074

Okay, I have Managed to get the values generated in the CRED form, though I'm struggling to get them saved to the database.

The following is the code I'm using...

[cred_generic_field field='vessel-authorised-reseller' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="authorised-resellers-dynamically-generated-for-cred"] ]
}
[/cred_generic_field]
add_action('cred_save_data', 'save_vessel_authorised_resellers',10,2);
function save_vessel_authorised_resellers($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==193)
    {
        if (isset($_POST['vessel-authorised-reseller']))
        {
            // add it to saved post meta
            add_post_meta($post_id, '__vessel-authorised-reseller', $_POST['vessel-authorised-reseller'], true);
        }
    }
}

The slug on the custom field (Single Line) created on the Vessel CPT is vessel-authorised-reseller

Can you advise on what may be the issue?

Also, if I'm to filter views conditionally based on these user IDs, is Single Line format best?

#579153

Ok, I have now got this to save through the forms, though existing values do not pre-populate in the Edit form.

Can you advise on how to show existing selections in the edit post CRED form?

The code I am using is as below...

add_action('cred_save_data', 'edit_vessel_save_authorised_reseller',10,2);
function edit_vessel_save_authorised_reseller($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==208)
        
    {
        $vals= isset( $_POST["vessel-authorised-reseller"] ) ? $_POST["vessel-authorised-reseller"] : array();
        foreach($vals as $val) {
          add_post_meta($post_id, 'wpcf-vessel-authorised-reseller', $val, false);
        }      
    }
}
#579238

You can setup the default value of as attribute "default", for example:

[cred_generic_field field='vessel-authorised-reseller' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":["a","c"],
"options":[
{"value":"a","label":"a"},
{"value":"b","label":"b"},
{"value":"c","label":"c"}
]
}
[/cred_generic_field]

Above generic field will be able to output a checkboxes fields with options "a", "b", "c", and setup option "a" and "c" as checked.

In your case, you will need to create another shortcode to get the field "__vessel-authorised-reseller" value from database
https://developer.wordpress.org/reference/functions/get_post_meta/
and output them into attribute "default"
https://developer.wordpress.org/reference/functions/add_shortcode/

#579486

Unfortunately this code is a little beyond me at the moment, is it possible you could provide an example for accessing and outputting the checkbox field values to the CRED form?

#579770

Here is a workaround,

1) Create a content template "test", with below codes:

[wpv-post-field name="wpcf-vessel-authorised-reseller" separator='", "']

2) Modify the CRED form, edit the codes from:

[cred_generic_field field='vessel-authorised-reseller' type='checkboxes' class='' urlparam='']
{
...

To:

[cred_generic_field field='vessel-authorised-reseller' type='checkboxes' class='' urlparam='']
{
"default":["[wpv-post-body view_template="test"]"],
...

And test again.

#580041

Looks like it's working, thanks!

#580082

You are welcome.