By jshwlkr


2018-02-16 19:12:44 8 Comments

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?

1 comments

@Nathan Johnson 2018-04-06 20:49:00

The WordPress was 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 );

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Multiple page plugin settings

0 Answered Questions

Allow users to add / remove settings in plugin

3 Answered Questions

[SOLVED] Where to store plugin settings fields

1 Answered Questions

[SOLVED] Category select options for plugin settings

1 Answered Questions

[SOLVED] Plugin settings are saving but the fields

0 Answered Questions

Plugin settings checkbox

1 Answered Questions

1 Answered Questions

[SOLVED] Does settings API create settings on run time?

1 Answered Questions

[SOLVED] Plugin settings not saving

1 Answered Questions

[SOLVED] GET parameters interfere with my plugin settings

Sponsored Content