Search code examples
javascriptphpwordpresswordpress-gutenberg

WordPress block how to access and save options values


I have some values stored in WordPress Option table that I usually manage with get_option/add_option/update_option/delete_option in php

is there any way to retrieve them with javascript in Gutenberg block or even to manage them?


Solution

  • You could pass the option as a localized javascript variable: https://wordpress.stackexchange.com/questions/294321/accessing-plugin-settings-in-gutenberg

    wp_localize_script('blocks-js', 'locals', array(
      'posts_per_page' => get_option('posts_per_page')
    ));
    

    However, I don't think there is currently a way to set options via Gutenberg. I assume it would be a defined store, but I haven't found anything. The documentation here is kind of lacking.

    In the meantime, you could set up a custom REST API endpoint that handles options changes or just a generic ajax call.