Accessing plugin settings in gutenberg

I’m trying to build a gutenberg block (via plugin) that interfaces with a third-party api via credentials. I’m unsure how to, or even if I can, access a plugin’s settings in gutenberg in order to grab a potential credentials field for use in the block. (I understand there’s potential to put something in the editor’s sidebar, but I need a persistent global setting that doesn’t have to be set with every block.) Am I missing something in the documentation or is this not possible yet?

2 s
2

The WordPress way to access PHP variables with JavaScript is to use wp_localize_script().

function wpse_enqueue_scripts(){
  wp_enqueue_script( 'wpse', PATH_TO . 'script.js' );
  wp_localize_script( 'wpse', 'credentials', $credentials );
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_scripts' );

Then in your JavaScript, you can access the credentials like

console.log( credentials );

Leave a Comment