Dynamically populate the options of a select custom field, base on global CF

Support MB Settings Page Dynamically populate the options of a select custom field, base on global CFResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #42752
    sandrinerodriguessandrinerodrigues
    Participant

    Hi,

    in my traveling website, I have several refund policies depending on the type of travel.

    I want the customer to be able to add new refund policy if needed. So in a setting page, I have made a group "type of travel (text) / description (Wysiwyg editor)" , clonable.

    Now in my CPT "travel", I want the customer to be able to select the type of travel in a select field, with the options populated with the "type of travel" he defined in the setting page.

    I hope this is clear... Is that possible in meta box ?

    Thanks in advance,

    #42757
    PeterPeter
    Moderator

    Hello,

    Meta Box supports adding the value in the admin area and outputting it in the frontend. In your case, it is possible. You can output all descriptions in your form and show/hide each description based on the type of travel selection with Javascript code, like tabs/accordion.

    Read more in the documentation https://docs.metabox.io/extensions/mb-settings-page/

    #42763
    sandrinerodriguessandrinerodrigues
    Participant

    Thank you for your reply,
    I can effectively manage to add the value in the admin area.

    My concern is if I can access this values when I set a Custom Field in a Custom Post Type as a "Select" > in the options I don't want to enter the values manually, but access to the options set by the client in the admin area.

    Is that possible, do you have any documentation on this subject ?
    Thanks

    #42765
    PeterPeter
    Moderator

    Hello,

    It is possible. You can create a custom callback function to get the settings page value and return an array of values and labels. For example:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    function your_prefix_function_name( $meta_boxes ) {
        $meta_boxes[] = [
            'title'  => __( 'My Custom Fields', 'your-text-domain' ),
            'id'     => 'my-custom-fields',
            'fields' => [
                [
                    'type'          => 'select_advanced',
                    'name'          => esc_html__( 'Supervisor', 'apms' ),
                    'id'            => 'supervisor_id',
                    'options'       => my_func9999(),
                ]
            ],
        ];
        return $meta_boxes;
    }
    
    function my_func9999() {
        $settings_page = get_option( 'option_name' );
        // do your stuff
        return [
                'a' => 'A',
                1 => 1,
                2 => 2,
                'b' => 'B'
           ];
    }
    #42798
    sandrinerodriguessandrinerodrigues
    Participant

    OK thank you, that's working fine !

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.