Display fileds from network settings page

Support MB Settings Page Display fileds from network settings pageResolved

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #30577
    gretchgretch
    Participant

    Hi, I am a new user of Meta box and despite the doc I struggle to display my fields.

    The fields are in a network setting page (I use a wp multisite) :

    <?php
    add_filter( 'mb_settings_pages', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $settings_pages ) {
        $settings_pages[] = [
            'menu_title'  => __( 'Map settings', 'your-text-domain' ),
            'option_name' => 'map',
            'position'    => 80,
            'class'       => 'map_settings',
            'style'       => 'no-boxes',
            'columns'     => 1,
            'network'     => true,
            'icon_url'    => 'dashicons-admin-site',
        ];
    
        return $settings_pages;
    }

    Here are the fields :

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'          => __( 'Countries', 'your-text-domain' ),
            'id'             => 'countries',
            'settings_pages' => ['map_settings'],
            'tabs'           => [
                'global_tab' => [
                    'label' => 'Global',
                    'icon'  => '',
                ],
                'spain_tab' => [
                    'label' => 'Spain',
                    'icon'  => 'admin-appearance',
                ],
            ],
            'fields'         => [
                [
                    'name'   => __( 'Media network', 'your-text-domain' ),
                    'id'     => $prefix . 'global_media_network',
                    'type'   => 'group',
                    'class'  => 'main-group',
                    'fields' => [
                        [
                            'name'   => __( 'Logos', 'your-text-domain' ),
                            'id'     => $prefix . 'global_media_network_images_group',
                            'type'   => 'group',
                            'class'  => 'sub-group',
                            'fields' => [
                                [
                                    'id'               => $prefix . 'global_media_network_group_images',
                                    'type'             => 'image_advanced',
                                    'max_file_uploads' => 4,
                                    'class'            => 'media_network_gallery',
                                ],
                            ],
                        ],
                        [
                            'name'   => __( 'CTA', 'your-text-domain' ),
                            'id'     => $prefix . 'global_media_network_cta',
                            'type'   => 'group',
                            'class'  => 'sub-group',
                            'fields' => [
                                [
                                    'name'    => __( 'CTA wording', 'your-text-domain' ),
                                    'id'      => $prefix . 'global_media_network_cta_wording',
                                    'type'    => 'text',
                                    'columns' => 6,
                                ],
                                [
                                    'name'    => __( 'CTA link', 'your-text-domain' ),
                                    'id'      => $prefix . 'global_media_network_cta_link',
                                    'type'    => 'url',
                                    'columns' => 6,
                                ],
                            ],
                        ],
                    ],
                    'tab'    => 'global_tab',
                ],
                [
                    'name'   => __( 'Media network', 'your-text-domain' ),
                    'id'     => $prefix . 'spain_media_network',
                    'type'   => 'group',
                    'class'  => 'main-group',
                    'fields' => [
                        [
                            'name'   => __( 'Logos', 'your-text-domain' ),
                            'id'     => $prefix . 'spain_media_network_images_group',
                            'type'   => 'group',
                            'class'  => 'sub-group',
                            'fields' => [
                                [
                                    'id'               => $prefix . 'spain_media_network_group_images',
                                    'type'             => 'image_advanced',
                                    'max_file_uploads' => 4,
                                    'class'            => 'media_network_gallery',
                                ],
                            ],
                        ],
                        [
                            'name'   => __( 'CTA', 'your-text-domain' ),
                            'id'     => $prefix . 'global_media_network_cta',
                            'type'   => 'group',
                            'class'  => 'sub-group',
                            'fields' => [
                                [
                                    'name'    => __( 'CTA wording', 'your-text-domain' ),
                                    'id'      => $prefix . 'global_media_network_cta_wording',
                                    'type'    => 'text',
                                    'columns' => 6,
                                ],
                                [
                                    'name'          => __( 'CTA link', 'your-text-domain' ),
                                    'id'            => $prefix . 'global_media_network_cta_link',
                                    'type'          => 'url',
                                    'columns'       => 6,
                                    'tooltip_input' => 'Enter URL (https://....)',
                                ],
                                [
                                    'name'       => __( 'page link', 'your-text-domain' ),
                                    'id'         => $prefix . 'post_tl47lu1cssi',
                                    'type'       => 'post',
                                    'post_type'  => ['page'],
                                    'field_type' => 'select_advanced',
                                ],
                            ],
                        ],
                    ],
                    'tab'    => 'spain_tab',
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Please can you show me how to display a text field and display all images from a "image advanced" with my configuration ?
    I guess i am missing something :/
    Thanks you very much

    --

    Otherwise I wonder if there a way to use MB views with fields from a setting page.

    #30582
    Long NguyenLong Nguyen
    Moderator

    Hi Eliott,

    1. You need to get the main group value from the network setting first.
    2. $global_media_network = rwmb_meta( 'global_media_network', ['object_type' => 'network_setting'], 'map');
      

      Follow this documentation https://docs.metabox.io/extensions/mb-settings-page/#getting-field-value

    3. Then access the sub-group and field value based on the ID. Here is the sample code:
    // Display images
    $global_media_network_group_images = $global_media_network['global_media_network_images_group']['global_media_network_group_images'];
    foreach ( $global_media_network_group_images as $image_id ) {
        $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'large' ) );
        echo '<img src="' . $image['url'] . '">';
    }
    
    // Display wording
    $global_media_network_cta_wording = $global_media_network['global_media_network_cta']['global_media_network_cta_wording'];
    echo $global_media_network_cta_wording;

    Follow this documentation https://docs.metabox.io/extensions/meta-box-group/#getting-sub-field-values

    #30584
    gretchgretch
    Participant

    Maybe my configuration wasn't the easiest one to start with. Thanks to you I think I have now a good base for the following.
    Thank you very much !

    #30735
    gretchgretch
    Participant

    Hi,

    Still on the same settings, the code above works very well but the image advanced field only works on the main site.

    On the others the image source is "unknow" as you can see :
    No images - image advanced (not network main site)

    How can I fix that and display the field across the network ?

    Thank you for your precious help

    #30779
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Thanks for your feedback.

    I will inform the development team to check this issue and get back to you later.

    #30781
    gretchgretch
    Participant

    Hi,

    I guess that means you get the same result on your side.
    I look forward to your return.

    Thank you very much

    #31416
    gretchgretch
    Participant

    Hi !

    More than a month later, any news about my issue ?
    I bought this plugin for this specific feature so I really need to make it work 🙏

    (btw the topic is tagged as "resolved" but it's not)

    Thanks a lot for your help

    #31422
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I have some useful information from the developer team:

    The problem happens due to some fields' values are saved into the database as object ID (image, file, post ...) and they are saved to the main site of the network to avoid duplicate object ID.

    So we need to switch to the main site to get the field value, here is the sample code https://pastebin.com/txYEyDvc

    #34170
    gretchgretch
    Participant

    Hi Long,

    Thank you. The code works fine but compared to the first one, it display the original file instead of the thumbnail size.
    Is it possible to adjust the last code to display thumbnails ?

    Thank you a lot

    #34187
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the WordPress function wp_get_attachment_image_url() to get the thumbnail URL based on the attachment (image) ID.

    foreach ( $global_media_network_group_images as $image_id ) {
        $image_url = wp_get_attachment_image_url( $image_id, 'medium' );
        echo '<img src="' . $image_url . '">';
    }

    Read more on the documentation https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/

    #34410
    gretchgretch
    Participant

    Perfect !
    Thank you very much for your help.

    #38962
    gretchgretch
    Participant

    Hello again,

    Since I have to update to PHP8 I have a strange behavior with my Metabox code.
    Everything is fine if all my fields are filled but if not my website crash with every empty fields returning this error : PHP Warning: Undefined array key "[name of the field]"

    Here is a sample of the code:

    add_filter( 'mb_settings_pages', function 	( $settings_pages ) {
    	$settings_pages[] = [
            'menu_title'  => __( 'Map settings', 'Map settings' ),
            'option_name' => 'map',
            'position'    => 80,
            'id'       => 'map_settings',
            'class'       => 'map_settings',
            'style'       => 'no-boxes',
            'columns'     => 1,
            'network'     => true,
            'icon_url'    => 'dashicons-admin-site',
        ];
    
    	return $settings_pages;
    } );
    
    add_filter( 'rwmb_meta_boxes', 'webedia_map_countries' );
    
    function webedia_map_countries( $meta_boxes ) {
      $prefix = '';
    
      $meta_boxes[] = [
        'title'          => __( 'Countries', 'map-countries' ),
        'id'             => 'countries',
        'settings_pages' => ['map_settings'],
        'tabs'           => [
          'global_tab'               => [
            'label' => 'International',
            'icon'  => '',
          ],
          'africa_tab'               => [
            'label' => 'Africa',
            'icon'  => '',
          ],
          'asia_tab'                 => [
            'label' => 'Asia',
            'icon'  => '',
          ],
          'brazil_tab'               => [
            'label' => 'Brazil',
            'icon'  => '',
          ],
          'france_tab'               => [
            'label' => 'France',
            'icon'  => '',
          ],
          'germany_tab'              => [
            'label' => 'Germany',
            'icon'  => '',
          ],
          'mexico_latin_america_tab' => [
            'label' => 'Mexico / Latin America',
            'icon'  => '',
          ],
          'middle_east_tab'          => [
            'label' => 'Middle East',
            'icon'  => '',
          ],
          'poland_tab'               => [
            'label' => 'Poland',
            'icon'  => '',
          ],
          'spain_tab'                => [
            'label' => 'Spain',
            'icon'  => '',
          ],
          'turkey_tab'               => [
            'label' => 'Turkey',
            'icon'  => '',
          ],
          'uk_tab'                   => [
            'label' => 'UK',
            'icon'  => '',
          ],
          'usa_tab'                  => [
            'label' => 'USA',
            'icon'  => '',
          ],
        ],
        'fields'         => [
          [
            'name'   => __( 'Media network', 'map-countries' ),
            'id'     => $prefix . 'global_media_network',
            'type'   => 'group',
            'class'  => 'main-group',
            'fields' => [
              [
                'name'    => __( 'Description group', 'map-countries' ),
                'id'      => $prefix . 'global_media_network_description_group',
                'type'    => 'group',
                'class'  => 'sub-group',
                'fields'  => [
                  [
                    'name' => __( 'Descriptionsss', 'map-countries' ),
                    'id'   => $prefix . 'global_media_network_description',
                    'type' => 'text',
                    
                  ],
                ],
              ],
              [
                'name'   => __( 'Logos', 'map-countries' ),
                'id'     => $prefix . 'global_media_network_images_group',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'id'               => $prefix . 'global_media_network_group_images',
                    'type'             => 'image_advanced',
                    'max_file_uploads' => 4,
                    'class'            => 'media_network_gallery',
                  ],
                ],
              ],
              [
                'name'   => __( 'CTA', 'map-countries' ),
                'id'     => $prefix . 'global_media_network_cta',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'name'    => __( 'CTA wording', 'map-countries' ),
                    'id'      => $prefix . 'global_media_network_cta_wording',
                    'type'    => 'text',
                    'columns' => 6,
                  ],
                  [
                    'name'    => __( 'CTA link', 'map-countries' ),
                    'id'      => $prefix . 'global_media_network_cta_link',
                    'type'    => 'url',
                    'columns' => 6,
                  ],
                ],
              ],
            ],
            'tab'    => 'global_tab',
          ],
          [
            'name'   => __( 'Creators', 'map-countries' ),
            'id'     => $prefix . 'global_creators',
            'type'   => 'group',
            'class'  => 'main-group',
            'fields' => [
              [
                'name'    => __( 'Description', 'map-countries' ),
                'id'      => $prefix . 'global_creators_description',
                'type'    => 'textarea',
                'columns' => 7,
              ],
              [
                'name'   => __( 'Logos', 'map-countries' ),
                'id'     => $prefix . 'global_creators_images_group',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'id'               => $prefix . 'global_creators_group_images',
                    'type'             => 'image_advanced',
                    'max_file_uploads' => 4,
                    'class'            => 'media_network_gallery',
                  ],
                ],
              ],
              [
                'name'   => __( 'CTA', 'map-countries' ),
                'id'     => $prefix . 'global_creators_cta',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'name'    => __( 'CTA wording', 'map-countries' ),
                    'id'      => $prefix . 'global_creators_cta_wording',
                    'type'    => 'text',
                    'columns' => 6,
                  ],
                  [
                    'name'    => __( 'CTA link', 'map-countries' ),
                    'id'      => $prefix . 'global_creators_cta_link',
                    'type'    => 'url',
                    'columns' => 6,
                  ],
                ],
              ],
            ],
            'tab'    => 'global_tab',
          ],
          [
            'name'   => __( 'Studios', 'map-countries' ),
            'id'     => $prefix . 'global_studios',
            'type'   => 'group',
            'class'  => 'main-group',
            'fields' => [
              [
                'name'    => __( 'Description', 'map-countries' ),
                'id'      => $prefix . 'global_studios_description',
                'type'    => 'textarea',
                'columns' => 7,
              ],
              [
                'name'   => __( 'Logos', 'map-countries' ),
                'id'     => $prefix . 'global_studios_images_group',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'id'               => $prefix . 'global_studios_group_images',
                    'type'             => 'image_advanced',
                    'max_file_uploads' => 4,
                    'class'            => 'media_network_gallery',
                  ],
                ],
              ],
              [
                'name'   => __( 'CTA', 'map-countries' ),
                'id'     => $prefix . 'global_studios_cta',
                'type'   => 'group',
                'class'  => 'sub-group',
                'fields' => [
                  [
                    'name'    => __( 'CTA wording', 'map-countries' ),
                    'id'      => $prefix . 'global_studios_cta_wording',
                    'type'    => 'text',
                    'columns' => 6,
                  ],
                  [
                    'name'    => __( 'CTA link', 'map-countries' ),
                    'id'      => $prefix . 'global_studios_cta_link',
                    'type'    => 'url',
                    'columns' => 6,
                  ],
                ],
              ],
            ],
            'tab'    => 'global_tab',
          ],
    
    ETC.....
    
        <?php $global_media_network = rwmb_meta( 'global_media_network', ['object_type' => 'network_setting'], 'map'); ?>
        <?php $global_creators = rwmb_meta( 'global_creators', ['object_type' => 'network_setting'], 'map'); ?>
        <?php $global_studios = rwmb_meta( 'global_studios', ['object_type' => 'network_setting'], 'map'); ?>
        <div class="country-content international">
            <div class="country-title">International</div>
            <div class="media-network">
                    <?php
                    $global_media_network_group_images = $global_media_network['global_media_network_images_group']['global_media_network_group_images'];
                    $global_media_network_description = $global_media_network['global_media_network_description_group']['global_media_network_description']; 
                    if ($global_media_network_group_images || $global_media_network_description) {
                        echo '<div class="media-network-title">Networks</div>';
                    };
                    ?>
                    <?php 
                        echo '<p>' . $global_media_network_description . '</p>';
                    ?>
    
            <div class="media-network-content">
                    <?php
                    $id = get_main_site_id();
                    switch_to_blog( $id );
                    $global_media_network_group_images = $global_media_network['global_media_network_images_group']['global_media_network_group_images'];
                    foreach ( $global_media_network_group_images as $image_id ) {
                        $image_url = wp_get_attachment_image_url( $image_id, 'thumbnail' );
                        echo '<img src="' . $image_url . '">';
                    }
                    restore_current_blog();
                    ?> 
                </div>
                <div class="country-content-cta">
                    <!-- CTA -->
                    <a href="<?php $global_media_network_cta_link = $global_media_network['global_media_network_cta']['global_media_network_cta_link']; echo $global_media_network_cta_link;?>"><?php $global_media_network_cta_wording = $global_media_network['global_media_network_cta']['global_media_network_cta_wording']; echo $global_media_network_cta_wording;?></a>
                </div>
            </div>
    
            <div class="creators">
                    <?php
                    $global_creators_group_images = $global_creators['global_creators_images_group']['global_creators_group_images'];
                    $global_creators_description = $global_creators['global_creators_description_group']['global_creators_description'];
                    if ($global_creators_group_images || $global_creators_description) {
                        echo '<div class="creators-title">Creators</div>';
                    };
                    ?>
                    <?php
                        echo '<p>' . $global_creators_description . '</p>';
                    ?>
    
            <div class="creators-content">
                    <?php
                    $id = get_main_site_id();
                    switch_to_blog( $id );
                    $global_creators_group_images = $global_creators['global_creators_images_group']['global_creators_group_images'];
                    foreach ( $global_creators_group_images as $image_id ) {
                        $image_url = wp_get_attachment_image_url( $image_id, 'thumbnail' );
                        echo '<img src="' . $image_url . '">';
                    }
                    restore_current_blog();
                    ?> 
                </div>
                <div class="country-content-cta">
                    <a href="<?php $global_creators_cta_link = $global_creators['global_creators_cta']['global_creators_cta_link']; echo $global_creators_cta_link;?>"><?php $global_creators_cta_wording = $global_creators['global_creators_cta']['global_creators_cta_wording']; echo $global_creators_cta_wording;?></a>
                </div>
            </div>
    
            <div class="studios">
                    <?php
                    $global_studios_group_images = $global_studios['global_studios_images_group']['global_studios_group_images'];
                    $global_studios_description = $global_studios['global_studios_description']; 
                    if ($global_studios_group_images || $global_studios_description) {
                        echo '<div class="studios-title">Studios</div>';
                    };
                    ?>
                    <?php 
                        echo '<p>' . $global_studios_description . '</p>';
                    ?>
    
            <div class="studios-content">
                    <?php
                    $id = get_main_site_id();
                    switch_to_blog( $id );
                    $global_studios_group_images = $global_studios['global_studios_images_group']['global_studios_group_images'];
                    foreach ( $global_studios_group_images as $image_id ) {
                        $image_url = wp_get_attachment_image_url( $image_id, 'thumbnail' );
                        echo '<img src="' . $image_url . '">';
                    }
                    restore_current_blog();
                    ?>
                </div>
                <div class="country-content-cta">
                    <a href="<?php $global_studios_cta_link = $global_studios['global_studios_cta']['global_studios_cta_link']; echo $global_studios_cta_link;?>"><?php $global_studios_cta_wording = $global_studios['global_studios_cta']['global_studios_cta_wording']; echo $global_studios_cta_wording;?></a>
                </div>
            </div>
        </div>
    

    How can I update the code to work with PHP8+ ?
    Do you need the full code to figure out the issue ?

    I really tried everything I could but I can't solve it :/

    Thanks you very much

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