Skip Navigation

[Resolved] Dynamically-populate CRED generic select field

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

Problem:
How to populate the CRED generic select field options dynamically.

Solution:
To dynamically create the CRED generic field options, you need to create a view that eventually outputs the JSON as views result. You can find the proposed solution with the following link:
=> https://toolset.com/forums/topic/dynamically-populate-*select-field-2/#post-525640

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

This support ticket is created 6 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Greig Neilson 6 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#525561

I am trying to dynamically populate a select field using content drawn from a CPT.

I have been using the method described here: https://toolset.com/forums/topic/dynamic-cred-generic-field-options/

I am confident that my view is working as I can display the correct values on the form's front end. It appears like this:
{“value”:”Lumsden School”,”label”:”Lumsden School”},{“value”:”Mossburn School”,”label”:”Mossburn School”},

I'm using the code below to get these dynamic values into the select field on the form but it just isn't working - the select menu does not appear.

[cred_generic_field field="_wpcf_belongs_school_id" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[[wpv-view name="select-enrolment-schools"],{"value":"1","label":"No Owner"}]
}
[/cred_generic_field]

Thanks in advance for your assistance 🙂

#525593

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please share problem URL where I can see above code working.

#525640

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've modified your CRED field as given under:

[cred_generic_field field="_wpcf_belongs_school_id" type="select" class="" urlparam=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"",
"options":[[wpv-view name="select-enrolment-schools"],{"value":"1","label":"No Owner"}] 
}
[/cred_generic_field]

And I've also modified your view's output as given under:

<wpv-loop>
  [wpv-item index=1]
  {"value":"[types field='school-name' id='$school'][/types]","label":"[types field='school-name' id='$school'][/types]"}
  [wpv-item index=other]
  ,{"value":"[types field='school-name' id='$school'][/types]","label":"[types field='school-name' id='$school'][/types]"}
</wpv-loop>

Now - I can see the dynamic dropdown on your CRED form. Could you please confirm.

#525643

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Also - I forget to add, I've added following filter to your code snippet:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
  
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '6979' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

You can find it here:
=> hidden link

#525656

Hi Minesh

Many thanks for your prompt reply. The solution looks great.

I'm not much of a coder, so could you please tell me in brief what that code snippet does? I'm going to need to add a second select field to that form so I'm guessing that I will need to modify the $id. I'm also guessing that I need to modify the filter title? Sorry for the dumb question - hooks are very new to me 🙂

Kind regards,
Greig

#525658

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

For multiple views you should add your view ids to field: $view_ids

I've adjusted following code on your site for multiple view ids.

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
    $view_ids = array(6979,'add-your-view-id-here');

    if ( in_array($id,$view_ids) ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Please replace 'add-your-view-id-here' with your view id, if you have more than one, view ids should be comma separated.

#525943

Excellent! Many thanks, Minesh 🙂

#525965

Hello again, Minesh

I realised that instead of saving the school and address as single line fields I could use the relationship between the post types. So I went and modified the views to populate the select fields with the correct value - the select fields now have the id of the parent as the value. I'm displaying these on the CRED form and they are correct.

This form is to create an Application CPT. The Application has three parents: Students, Student locations, Schools.

When saving the application the Students parent is successfully saved, but the Student location and the School parent are not. Here's the code for one of those that isn't working:

[cred_generic_field field="_wpcf_belongs_schools_id" type="select" class="" urlparam="" value=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"",
"options":[[wpv-view name="select-enrolment-schools"]]
}
[/cred_generic_field]

#526053

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As your original issue is resolved and as per our support policy we entertain only one question per support ticket. May I kindly ask you to open a new ticket with your each new question, this will help other users searching on the forum.

Thank you for understanding.

#526580

Thanks 🙂

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