Skip Navigation

[Resolved] How to replace a cred form select input field with a php snippet

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

Problem:
Customer wanted to know how to programmatically generate select options in CRED form

Solution:
Suggested to make use of "wpt_field_options" filter with an example snippet

This support ticket is created 6 years, 3 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 – 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/Karachi (GMT+05:00)

Author
Posts
#1115476

Hi,

I have created a new post type with a new field "production-year", as a required select field, with 2018, 2017, 2016 as options. Also I have built a cred form to add new content. In this form, I need to programmaticaly build the select year options (current year and two previous years) with a php snippet (I am aware of the php code to use).
What is the recommended way to tackle this? Please provide any guidance.

Thanks in advance,
Kostas

#1116078

Anyone?

#1116135

Hi Kostas,

Thank you for contacting us and I'll be happy to assist.

To programmatically generate select field options (into admin area custom fields as well as on the front end through CRED form), you can use “wpt_field_options” filter: e.g.

add_filter( 'wpt_field_options', 'func_to_dynamically_populate_production_year', 10, 3);
function func_to_dynamically_populate_production_year( $options, $title, $type ){
    switch( $title ){
        case 'Production Year':
            $options = array();
            // add or generate the array of your desired option titles and values
            $options[] = array('#value' => '', '#title' => '---',);
            break;
    }
    return $options;
}

Note: Please make sure to update ‘Production Year’ with the actual “Field name” (not slug) of your custom field

I hope this helps! Please let us know if you need any further assistance.

#1116171

Hi waqar.a,

Thank you for responding, your suggested code works fine!
However, according to this:

https://toolset.com/forums/topic/title-in-hook-wpt_field_options-not-working-with-term-field/

wpt_field_options filter is undocumented and not officially supported.
Does this mean that it can stop working anytime in the future?

Thanks again,
Kostas

#1116241

Hi Kostas,

Thank for the update.

Glad to know that solution I shared helped you to resolve your issue.

We do have plans to document that filter and we do have an internal ticket for that. Feel free to use that filter, it should work without any issue.

#1116266

Excellent, thank you waqar.a!

Best Regards,
Kostas