Custom Fields
Re-Envisioned
Support MB Settings Page Code not working with updated version

  • Creator
    Topic
  • #22338
    marcodedo
    Participant

    Hi with the new version of the MB settings page plugin I have a problem that I did not have with the previous one.

    In version 1.3.4 I set this code to get a dynamic checkbox list controlled through the setting page:

    <?php
    /*------------------------------------*\
        SETTING PAGE
    \*------------------------------------*/
    add_filter( 'mb_settings_pages', 'settings_pages' );
    function settings_pages( $settings_pages ) {
    
        $settings_pages[] = array(
            'id'            => 'options',
            'menu_title'    => 'Settings',
            'title'         => 'Option page',
            'option_name'   => 'options',
            'icon_url'      => 'dashicons-images-alt',
            'submenu_title' => 'Option page',
            
            'tabs'        => array(
                'allergens'  => 'Allergens',
            ),
        );
        return $settings_pages;
    }
    
    add_filter( 'rwmb_meta_boxes', 'options_meta_boxes' );
    function options_meta_boxes( $meta_boxes ) {
    
        $meta_boxes[] = array(
            'id'                =>  'allergens',
            'title'             =>  'Allergens',
            'settings_pages'    =>  'options',
            'tab'               =>  'allergens',
    
            'fields'    =>  array(
                array(
                    'id'      => 'list',
                    'name'    => 'List',
                    'type'    => 'text',
                    'clone' => true,
                    'add_button' => '+ More',
                ),
            ),
        );
        
        return $meta_boxes;
    }
        
    
    # ============================== #
    # ===    METABOX - FIELDS    === #
    # ============================== #
    
    add_filter( 'rwmb_meta_boxes', 'mbfieldse_list' );
    function mbfieldse_list( $meta_boxes ) {
    
        $allergens = rwmb_meta( 'list', array( 'object_type' => 'setting' ), 'options' );
    
        if ($allergens) {
            //Combine Array
            $listallergens = array_combine($allergens, array_values($allergens));
        } else {
            // if $allergens is empty
            $listallergens = null;
        }
    
        $meta_boxes[] = array(
            'title'          => 'My List',
            
            'fields' => array(
    
                        array(
                            'name'    => 'List',
                            'id'      => 'allergensList',
                            'type'    => 'checkbox_list',
                            'options' => $listallergens,
    
                            'select_all_none' => true,
                            
                        ),
                    ),                
                ),
            ),
        );
        
        return $meta_boxes;
        
    }

    With MB setting page v1.3.4 everything worked fine, but updating the plugin to v2.1.3 the variable $allergens remains empty (NULL).

    Maybe I am wrong something?

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #22340
    Long Nguyen
    Moderator

    Hi,

    Because the helper function rwmb_meta() runs after the filter hook rwmb_meta_boxes so you can use the function get_option() to get settings page value inside the function callback.

    $allergens = get_option( 'options' )['list'];

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