Block doesn't render on frontend

Support MB Blocks Block doesn't render on frontendResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17762
    JackkyJackky
    Participant

    Hi! I'm trying to male my first block and it's working perfect on backend, but on frontend appers nothing. I can't find out why.

    Block template:

    <?php
    echo 'test output';
    if ( empty( $attributes['data'] ) ) return;
    
    $data = to_object($attributes['data']);
    ?>
    
    <?php if ($data->triggers) { ?>
    <div class="text-trigger">
        <?php foreach ($data->triggers as $trigger) { ?>
        <div class="text-trigger__item">
            <div class="text-trigger__item-inner">
                <div class="text-trigger__item-icon" style="background-image: url(<?= wp_get_attachment_image_url($trigger->icon, 'icon-0x100'); ?>);"></div>
                <div class="text-trigger__item-text">
                    <?= $trigger->text; ?>
                </div>
            </div>
        </div>
        <?php } ?>
    </div>
    <?php } ?>

    MetaBox:

    <?php
    
    $meta_boxes[] = [
        'title'           => 'Текстовые триггеры',
        'id'              => 'text-trigger',
        //'description'     => 'A custom hero content block',
        'type'            => 'block',
        'icon'            => 'awards',
        'category'        => 'layout',
        'context'         => 'side',
        'render_template' => get_template_directory() . '/gutenberg/text-trigger.php',
        'supports' => [
            'align' => ['wide', 'full'],
        ],
        'fields'          => [
            [
                'id'                => 'triggers',
                'type'              => 'group',
                'name'              => 'Контент',
                'clone'             => true,
                'sort_clone'        => true,
                'add_button'        => '+',
                'fields'            => [
                    [
                        'id'            => 'icon',
                        'type'          => 'single_image',
                        'name'          => 'Иконка',
                    ],
                    [
                        'id'            => 'text',
                        'type'          => 'text',
                        'name'          => 'Текст',
                    ],
                ]
            ],
        ],
    ];
    #17763
    JackkyJackky
    Participant

    I have find out the issue. It's because I'm wrapping init of mb in is_admin condition like this:

    if (is_admin()) {
        add_filter( 'rwmb_meta_boxes', 'register_meta_boxes' );
        add_filter( 'mb_settings_pages', 'settings_pages' );
    }

    How can I load render template on frontend with saving this condition?

    #17779
    Anh TranAnh Tran
    Keymaster

    Hi Jackky,

    The condition is_admin() should never be used. It prevents the fields/settings available for retrieving the data on the front end.

    #17782
    JackkyJackky
    Participant

    Yeah, I know, but it's likely for me to disable every functions that I'm not using in frontend for security purposes. So I wanna make a suggestion to make some filters for MB, that allows to load only that functions I need, like rendering block template, not all helpers.

    #17784
    Anh TranAnh Tran
    Keymaster

    I'm afraid it's not possible. All meta boxes and fields when registered will be added to a registry. That makes the helper functions to be able to find them to get the field settings and get field value. It's an integral part of the plugin and can't be disabled.

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