Generate checkbox list choices with callback

Support General Generate checkbox list choices with callback

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #47214
    YumikomYumikom
    Participant

    Hi,

    Generating checkbox list choices with callback, but all other field values are returned false in the function I defined.
    This is the checkbox list used in the site settings page.

    Is it possible to get the stored values of the other fields?

    #47220
    PeterPeter
    Moderator

    Hello Yumiko,

    Can you please share more details of the issue on your site? Because I don't know what exactly the issue is. Let me know the callback function, other field values, settings page ... and screenshots of them.

    #47222
    YumikomYumikom
    Participant

    Hi Peter,

    The test code is as follows

    What we want to achieve
    * Generate the contents of the checkbox list in the callback of test_B depending on the status of the checkbox in test_A.

    Defects
    * When the value of test_A is retrieved in the callback of test_B, it is always false as well.

    
    <?php
    add_filter( 'rwmb_meta_boxes', 'example' );
    
    function example( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'          => __( 'test field group', 'example-domain' ),
            'id'             => 'test1',
            'settings_pages' => ['custom-options'],
            'fields'         => [
                [
                    'name'              => __( 'test A', 'example-domain' ),
                    'id'                => $prefix . 'test_a',
                    'type'              => 'checkbox',
                    'std'               => false,
                    'required'          => false,
                    'clone'             => false,
                    'clone_empty_start' => false,
                ],
                [
                    'name'              => __( 'test B', 'example-domain' ),
                    'id'                => $prefix . 'test_b',
                    'type'              => 'checkbox_list',
                    'options'           => test_function(),
                    'inline'            => false,
                    'select_all_none'   => false,
                    'required'          => false,
                    'clone'             => false,
                    'clone_empty_start' => false,
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    
    
    function test_function() {
    	$value_a = rwmb_meta( 'test_A', array( 'object_type' => 'setting' ), 'custom-options' );
    
    	$optionsA = array(
    		'1' => 'Apple',
    		'2' => 'Orange'
    	);
    
    	$optionsB = array(
    		'3' => 'Cat',
    		'4' => 'Dog'
    	);
    
    	return $value_a ? $optionsA : $optionsB;
    }
    
    #47226
    PeterPeter
    Moderator

    Hello,

    Please ensure the option name is added to the helper function, and the separator should be underscore instead of dash. For example custom_options

    
    $value_a = rwmb_meta( 'test_A', array( 'object_type' => 'setting' ), 'custom_options' );
    

    Following the documentation https://docs.metabox.io/extensions/mb-settings-page/#using-code

    After you save the field test_a value, the $optionsA will be returned.

    #47227
    YumikomYumikom
    Participant

    Hi,

    The defect is not related to the name of the settings ID.
    It is the correct setting in my code.

    I am specifying Metabox for the Settings page configured in the code below.

    
    define( 'SETTINGS_OPTION_NAME', 'custom-options' );
    function com_options_page( $settings_pages ) {
    	$settings_pages[] = array(
    		'id'          => SETTINGS_OPTION_NAME,
    		'option_name' => SETTINGS_OPTION_NAME,
    		'menu_title'  => 'サイト設定',
    		'icon_url'    => 'dashicons-edit',
    		'style'       => 'no-boxes',
    		'columns'     => 1,
    		'position'    => 2,
    	);
    	return $settings_pages;
    }
    add_filter( 'mb_settings_pages', 'com_options_page' );
    
    

    I make sure that $value_a is always false.
    Can you please confirm the behavior of this bug?

    #47233
    PeterPeter
    Moderator

    Hello,

    I understand the issue. It is related to this topic https://support.metabox.io/topic/dynamically-populate-the-options-of-a-select-custom-field-base-on-global-cf/

    you need to use the WordPress function get_option() to get the settings page value (like the theme option) when creating the custom field. At this point, the helper function rwmb_meta() doesn't work.

    Here is an example

    function test_function() {
        $options = get_option( 'custom-options' );
        $value_a = $options['test_a'];
    
        $optionsA = array(
            '1' => 'Apple',
            '2' => 'Orange'
        );
    
        $optionsB = array(
            '3' => 'Cat',
            '4' => 'Dog'
        );
    
        return $value_a ? $optionsA : $optionsB;
    }

    If you use the builder, you will need to re-update the field group to update the choice for the field test_b.

    #47235
    YumikomYumikom
    Participant

    Hi peter,

    It could be obtained with get_options().
    Thank you for sharing this information.

    It would be helpful if you could also mention in the document that rwmb_meta() is not available in callback.

    Thanks again.

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