Created
January 4, 2021 11:37
-
-
Save BenBroide/9c5e2de86fbee7461b40b813b0f78001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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