Skip to content

Instantly share code, notes, and snippets.

@BenBroide
Created January 4, 2021 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenBroide/9c5e2de86fbee7461b40b813b0f78001 to your computer and use it in GitHub Desktop.
Save BenBroide/9c5e2de86fbee7461b40b813b0f78001 to your computer and use it in GitHub Desktop.
<?php
function sgf_load_scripts() {
$dir = __DIR__;
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "create-block/simple-guten-fields" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require( $script_asset_path );
wp_register_script(
'sgf-script',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
$fields = apply_filters( 'sgf_register_fields', [] );
$data = [
'fields' => $fields,
];
wp_localize_script( 'sgf-script', 'sgf_data', $data );
wp_enqueue_script('sgf-script');
}
add_action( 'admin_enqueue_scripts', 'sgf_load_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment