block editor - Gutenberg sidebar example does not update meta field in the database

Gutenberg example for creating sidebardoes not keep what is entered in the box. After refreshing the page, entered co

Gutenberg example for creating sidebar / does not keep what is entered in the box. After refreshing the page, entered content is gone end text box is empty. I guess it is some kind of REST problem?

I just checked, when updating post, meta field is sent via POST request, therefore it is not a REST problem:

{"meta":{"sidebar_plugin_meta_block_field":"Jozica1"},"content":" ... etc

PHP Code:

add_action( 'init', function () {
    if (is_admin()) {
        wp_register_script( 'gmk-sidebar-js', get_template_directory_uri() . '/js/sidebar-panel.js',
        [ 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data' ]
        );
        wp_register_style( 'gmk-sidebar-style', get_template_directory_uri() . '/css/admin-guten-sidebar.css' );

        register_post_meta( '', 'sidebar_plugin_meta_block_field', array(
            'show_in_rest' => true,
            'single' => true,
            'type' => 'string',
        ) );
    }
} );

Gutenberg example for creating sidebar https://wordpress/gutenberg/handbook/designers-developers/developers/tutorials/plugin-sidebar-0/ does not keep what is entered in the box. After refreshing the page, entered content is gone end text box is empty. I guess it is some kind of REST problem?

I just checked, when updating post, meta field is sent via POST request, therefore it is not a REST problem:

{"meta":{"sidebar_plugin_meta_block_field":"Jozica1"},"content":" ... etc

PHP Code:

add_action( 'init', function () {
    if (is_admin()) {
        wp_register_script( 'gmk-sidebar-js', get_template_directory_uri() . '/js/sidebar-panel.js',
        [ 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data' ]
        );
        wp_register_style( 'gmk-sidebar-style', get_template_directory_uri() . '/css/admin-guten-sidebar.css' );

        register_post_meta( '', 'sidebar_plugin_meta_block_field', array(
            'show_in_rest' => true,
            'single' => true,
            'type' => 'string',
        ) );
    }
} );
Share Improve this question edited Apr 25, 2019 at 20:45 Lovor asked Apr 24, 2019 at 21:04 LovorLovor 1,00610 silver badges16 bronze badges 2
  • Please don't add the answer in your question please add a proper answer below. – norman.lol Commented Apr 25, 2019 at 20:18
  • Possible duplicate of wordpress.stackexchange/q/329760/30597 – norman.lol Commented Apr 25, 2019 at 20:20
Add a comment  | 

1 Answer 1

Reset to default 2

Registration of meta field must happen outside of admin area check. I had it enclosed in if (is_admin) check, so it was only happening when it passed this test. This was incorrect.

The question is duplicate and original question and answer are here: Plugin Sidebar is not Saving Meta Attribute to Post/Page after "Update"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745551424a4632593.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信