Skip Navigation

[Resolved] dynamic select options for cred user form fields

This support ticket is created 6 years, 9 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
- 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 8 replies, has 2 voices.

Last updated by emanuelaB 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#624397

I would like to set dynamic option values for a select field for a cred user form field, using a shortcode or a view, just like I can do with cred_generic_field.
Is it possible?
Alternatively, is it possible to save a cred_generic_field as user field value in the database?
Thanks in advance, Luca

#624490

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - is your user field is select field?

Could you please try to add following code to your theme's functions.php file.

For example:

add_filter( 'wpt_field_options', 'add_some_options', 10, 3);
//dynamically populate select field from Types
function add_some_options( $options, $title, $type ){
    switch( $title ){
        case 'Status':
         
            $options = array();
            $speakers = get_users(array( 'role' => 'Subscriber' ));
         
            foreach ($speakers as $user) {
                $options[] = array(
                    '#value' => $user->ID,
                    '#title' => $user->user_login,
                );
            }
            break;
    }
    return $options;
}

Where:
=> You should change 'Status' to your custom field status.
=> You should change the "Subscriber" to your desired role.

Please feel free to adjust above code as per your need.

Alternatively, is it possible to save a cred_generic_field as user field value in the database?
==> What if you use CRED hook cred_save_data and use update_user_meta() function to update the value for your generic field accordingly.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#624583

Hi
thank you very much, filter solutions works fine, I'm able to set select options with php.

There's just a little problem in WordPress User editor

I can see (and modify) option values for types user fields with option manually defined in User Field Group editor, but for user fields with options defined via php the value always seems to be empty, even if wpcf-<fieldname> meta values is ok in wp_usermeta table.

It seems that wpt_field_options works only with cred forms: is there any other filter or method I can use?

Thanks again, Luca

#624586

Minesh
Supporter

Languages: English (English )

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

Well - please note that wpt_field_options will work for fields created using Types - it will not work for native WordPress fields.

Which field you are trying to access?

#624646

I'm using two Types select user fields, and everything is ok even in WordPress Standard User editor, now: I just have to delete and re-create fields...
Thanks for your help.
Best regards

#624897

Minesh
Supporter

Languages: English (English )

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

Do you mean you want to delete and recreate the field with same name?

#624920

We have two fields, let's say wpcf-field1 and wpcf-field2: the first one has static select options defined via wp-types user group fields editor, the second one has no options, all options are managed with wpt_field_options filter.
In WordPress standard User Editor we were able to see only wpcf-fields1 select options, wpcf-field2 select seems to be empty.
I don't know why, but we've deleted and recreate fields with the same name and now everything works fine...
Thanks again, Luca

#624922

Minesh
Supporter

Languages: English (English )

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

Ok - glad to know that now it works for you. Could you please resolve this ticket.

Please feel free to open a new ticket with your each new question you may have.

#624926

Thanks Minesh for your help
Best regards, Luca