Gutenberg,  WordPress

Passing a PHP Variable to a Gutenberg Block

My challenge was how to apply a background image to a <div> in the Gutenberg admin panel. My solution was to add a variable to the head of the document that could be referenced in the block’s Javascript. The background image was then hardcoded as an inline style.

function myPluginUrl_Declare() {
    echo "<script>const MY_PLUGIN_URL='".plugin_dir_url( __DIR__ )."';</script>";
}
add_action( 'admin_head', 'myPluginUrl_Declare' );

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.