Custom Fields
Re-Envisioned
Support General How to get field settings

  • This topic has 6 replies, 2 voices, and was last updated 3 weeks ago by Alex.
  • Creator
    Topic
  • #20743
    Resolved Alex
    Participant

    Hi,
    Probably a silly question. I am trying to get options from a custom field type of Select using rwmb_get_field_settings(). The call is made with REST from front-end to the custom endpoint. Calling rwmb_get_field_settings() in the functions.php return false. Any ideas?
    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #20751
    Long Nguyen
    Moderator

    Hi Alex,

    Did you pass the arguments to the function rwmb_get_field_settings( $field_id, $args, $object_id )?

    Please share the code that you create the field and get the field settings. I will take a closer look and give a quick response.

    #20754
    Alex
    Participant

    Hi Long,
    Yes, I did use $field_id. It seems none of the helper functions work. I tried a few other with no success. The fields are visible in the Post. I can change the values.

    Here is just part of the Metabox.

    function.php

    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes');
    function prefix_register_meta_boxes($meta_boxes) {
        if (!class_exists('RW_Meta_Box')) {
            return;
        }
        /*=================================================
        * Properties Custom Type Metabox
        ===================================================*/
        $meta_boxes[] = array(
            'id' => 'property-meta-box',
            'title' => esc_html__( 'Property', 'textdomain' ),
            'pages' => array('property'),
            'tabs' => array(
                'property_location' => array(
                    'label' => esc_html__('Location', 'textdomain'),
                    'icon' => 'dashicons-location',
                ),
            ),
            'tab_style' => 'left',            
            'fields' => array(
                array (
                    'id' => 'country',
                    'name' => esc_html__( 'Country', 'textdomain' ),
                    'type' => 'select',
                    'placeholder' => esc_html__( 'Select an Item', 'textdomain' ),
                    'options' => array(
                        1 => esc_html__( 'Canada', 'textdomain' ),
                        2 => esc_html__( 'United States', 'textdomain' ),
                    ),
                    'tab' => 'property_location',
                    'columns' => 12,
                ),
    ),
    );
    return $meta_boxes;
    }
    
    $field = rwmb_get_field_settings('country');
    
    #20757
    Long Nguyen
    Moderator

    Hi Alex,

    If you are using the helper function rwmb_get_field_settings() outside the loop, you have to pass the argument $object_id (post ID) to this function.

    $field = rwmb_get_field_settings( 'country', '', 1234 );

    For more information, please follow this documentation https://docs.metabox.io/rwmb-get-field-settings/#arguments.

    #20759
    Alex
    Participant

    I have front-end React app. I am using WordPress as headless CMS. The post doesn’t exists yet. Hence I do not have post_id to pass it to the function. The idea is when a user opens form to create a new post, one of the fields is the Country select field. I was hoping to get options for this front-end field by fetching options from the custom Country back-end field. It is not a direct access. My app fires REST request to the custom endpoint. The custom endpoint function uses rwmb_get_field_seetings to get options and send it back to the from-end app. Is it even possible with Metabox? It did work with ACF.

    Thanks.

    #20762
    Long Nguyen
    Moderator

    Hi,

    I think there are two ways to achieve your goal:

    • Use the extension MB Settings Page, create some fields in a settings page, and get the field settings by using this code.

    $field = rwmb_get_field_settings( $field_id, ['object_type' => 'setting'], $option_name );

    Follow this documentation for more information https://docs.metabox.io/extensions/mb-settings-page/.

    • Just create a specific post and get the field settings from this post.

    Hope that helps.

    #20783
    Alex
    Participant

    Thank you Long. That should work.

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