Custom Fields
Re-Envisioned
Support MB Blocks enqueue_assets in block editor only

  • Creator
    Topic
  • #31829
    Resolved Nicholas 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;
            } );
        }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #31871
    Long 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 Cox
    Participant

    thanks! worked for me

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