enqueue_assets in block editor only

Support MB Blocks enqueue_assets in block editor onlyResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31829
    Nicholas CoxNicholas Cox
    Participant

    Hi

    I was wondering can I include css/js inside a custom block which will only load inside the block editor? so no front end loading.

    I do not want to include these within my theme or inside one css/js file, I ideally wanted to call this inside the setup of the meta block. Can i replace 'wp_enqueue_style' with 'enqueue_block_editor_assets'?

    
    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
                $meta_boxes[] = [
                    'title'           => 'Product Listing Block',
                    'id'              => 'dc-product-listing-block',
                    'description'     => 'Product listings for your store',
                    'type'            => 'block',
                    'icon'            => 'screenoptions',
                    'category'        => 'dc-blocks',
                    'context'         => 'side',
                    'render_template' => __DIR__ . '/template.php',
                    'enqueue_assets' => function() {
                        wp_enqueue_style( 'example', 'folder/example.css', [], '1.8.1' );
                    },
                    
                ];
                return $meta_boxes;
            } );
        }
    
    #31871
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please notice that enqueue_block_editor_assets is an action hook. See this example to know how to enqueue script files in your theme.
    https://gist.github.com/zgordon/2c8506a1dd1d6068b84fb8f88907bdb9

    #32116
    Nicholas CoxNicholas Cox
    Participant

    thanks! worked for me

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