Customizer Settings and fields

Support MB Settings Page Customizer Settings and fieldsResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #48180
    webdevwebdev
    Participant

    Hi,
    I've set up a customizer section using MB Settings and using the following code:

    add_filter( 'mb_settings_pages', 'wcpp_masthead_text' );
    
    function wcpp_masthead_text( $settings_pages ) {
    	$settings_pages[] = [
            'menu_title'      => __( 'Homepage Intro', 'wcpp' ),
            'id'              => 'homepage-masthead-text',
            'capability'      => 'edit_published_pages',
            'option_name'   => 'theme_mods_masthead_text',
            'style'           => 'no-boxes',
            'columns'         => 1,
            'customizer'      => true,
            'customizer_only' => true,
            'network'         => false,
        ];
    
    	return $settings_pages;
    }
    
    add_filter( 'rwmb_meta_boxes', 'wcpp_masthead_field' );
    
    function wcpp_masthead_field( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'          => __( 'Homepage Intro Text', 'wcpp' ),
            'id'             => 'homepage-masthead-wysiwyg',
            'settings_pages' => ['homepage-masthead-text'],
            'fields'         => [
                [
                    'name'              => __( 'Masthead Text', 'wcpp' ),
                    'id'                => $prefix . 'masthead_text',
                    'type'              => 'wysiwyg',
                    'raw'               => false,
                    'required'          => false,
                    'clone'             => false,
                    'hide_from_rest'    => false,
                    'hide_from_front'   => false,
                    'limit'             => 150,
                        'wpautop' => false,
                    'limit_type'        => 'character',
                    'options'         => [
                        'media_buttons' => true,
                        'teeny'         => false,
                        'tinymce'       => [
                            'toolbar1' => 'bold,italic,underline,|,bullist,numlist,|,link,unlink',
                            'toolbar2' => '',
                            'toolbar3' => '',
                            'toolbar4' => '',
                            'toolbar5' => '',
                            'wpautop' => false,
                        ],
                        'quicktags'    => false,
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    

    This works fine and I can retrieve the content, but in order to get to the fields in the Customizer, I have to click on 'Homepage Intro', which then shows me the 'Homepage Intro Text' accordion control, which I have to click in order to see the WYSIWYG field.

    How can I set it up so I get straight to the fields on the first click, ie, on clicking 'Homepage Intro', like the default WordPress controls?

    Also, could I allow users to add content using Blocks (standard WP blocks rather than anything created using MB Blocks) instead of the WYSIWYG in the customizer?

    #48181
    PeterPeter
    Moderator

    Hello,

    You can replace the meta box setting

    'settings_pages' => ['homepage-masthead-text'],

    to

    'panel' => '', to show the top-level section in the customizer

    $meta_boxes[] = [
        'title'          => __( 'Homepage Intro Text', 'wcpp' ),
        'id'             => 'homepage-masthead-wysiwyg',
        'panel' => '',
    ...
    ]

    Please follow the documentation https://docs.metabox.io/extensions/mb-settings-page/#top-level-sections

    Regarding the blocks, if you want to use the Meta Box custom fields with blocks, you need to create/register a custom block to use it. There isn't an option to use the custom fields with standard WordPress blocks.

    #48293
    webdevwebdev
    Participant

    Apologies for the late reply...
    That seems to work for showing the field in the correct place, but I can't work out how to retrieve the value.

    I have removed the entire 'mb_settings_pages' filter and function as suggested in the docs you pointed out, so my function is now just this:

    <?php
    
    add_filter( 'rwmb_meta_boxes', 'wcpp_masthead_field', 1 );
    
    function wcpp_masthead_field( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'          => __( 'Homepage Intro Text', 'wcpp' ),
            'id'             => 'homepage-masthead-wysiwyg',
            'panel'         => '',
            'fields'         => [
                [
                    'name'              => __( 'Masthead Text', 'wcpp' ),
                    'id'                => $prefix . 'masthead_text',
                    'type'              => 'wysiwyg',
                    'raw'               => false,
                    'required'          => false,
                    'clone'             => false,
                    'hide_from_rest'    => false,
                    'hide_from_front'   => false,
                    'limit'             => 150,
                        'wpautop' => false,
                    'limit_type'        => 'character',
                    'options'         => [
                        'media_buttons' => true,
                        'teeny'         => false,
                        'tinymce'       => [
                            'toolbar1' => 'bold,italic,underline,|,bullist,numlist,|,link,unlink',
                            'toolbar2' => '',
                            'toolbar3' => '',
                            'toolbar4' => '',
                            'toolbar5' => '',
                            'wpautop' => false,
                        ],
                        'quicktags'    => false,
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    If I show all options available, then I can see the relevant content under an option called 'theme_mods_[my-theme-slug]' but this only works on ym developemnt machine, presumably because of an old setting, as it is not available on the live version. I can't tell from the docs which is the option name I need to call, or if I'm missing something in the definition of the Customizer panel above.

    Thanks for your help.

    #48296
    PeterPeter
    Moderator

    Hello,

    In most cases, the option name that stores the customizer field value is theme_mods_themeSlug. If you don't know what the option name is, you can access the database and search for some keywords in the field value, table wp_options.

    #48299
    webdevwebdev
    Participant

    Hmm,

    It's really weird... On my dev site, the option is saved under that name, but on the live site, that setting does not exist, and if I

    var_dump(wp_load_alloptions());

    (which shows all saved options), there is nothing containing anything like the expected content.

    But the content is definitely being saved because if I go to the Customizer, edit the wysiwyg, save and then reload, the content is still there...

    Do you have any other ideas? Is it just being saved by MetaBox in a meta field somewhere?

    #48303
    webdevwebdev
    Participant

    Sorted now... my mistake... I'd downloaded the theme from our Git repo and it was zipped up as '[my-them-slug]-main.zip' with a corresponding 'main' folder inside.
    When uploaded without the 'main' part added by Git, it all works as expected

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