Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Get or set values in post meta

In the end, I’ve managed to do in this way

(function( plugins, editPost, element, components, data, compose ) {

    const el = element.createElement;

    const { Fragment } = element;
    const { registerPlugin } = plugins;
    const { PluginSidebar, PluginSidebarMoreMenuItem } = editPost;
    const { PanelBody, ColorPicker } = components;
    const { withSelect, withDispatch } = data;

    const MetaThemeControl = compose.compose(
        withDispatch( function( dispatch, props ) {
            return {
                setMetaValue: function( metaValue ) {
                    dispatch( 'core/editor' ).editPost(
                        { meta: { [ props.metaKey ]: metaValue } }
                    );
                }
            }
        } ),
        withSelect( function( select, props ) {
            return {
                metaValue: select( 'core/editor' ).getEditedPostAttribute( 'meta' )[ props.metaKey ],
            }
        } ) )( function( props ) {
            return el( ColorPicker, {
                label: props.title,
                value: props.metaValue,
                onChangeComplete: function( content ) {
                    props.setMetaValue( content.hex );
                },
            });
        }
    );

    registerPlugin( 'b-theme-sidebar', {
        render: function() {
            return el( Fragment, {},
                el( PluginSidebarMoreMenuItem,
                    {
                        target: 'b-theme-sidebar',
                        icon: 'admin-appearance',
                    },
                    'B Theme'
                ),
                el( PluginSidebar,
                    {
                        name: "b-theme-sidebar",
                        title: "B Theme",
                        icon: 'admin-appearance',
                    },
                    el( PanelBody, {},
                        // primary color
                        el( MetaThemeControl,
                            {
                                metaKey: 'primary_theme',
                                title : 'Primary Theme',
                            }
                        ),
                        // secondary color
                        el( MetaThemeControl,
                            {
                                metaKey: 'secondary_theme',
                                title : 'Secondary Theme',
                            }
                        ),
                    )
                )
            );
        }
    } );
} )(
    window.wp.plugins,
    window.wp.editPost,
    window.wp.element,
    window.wp.components,
    window.wp.data,
    window.wp.compose
);

But I would like to find a solution for my first attempt, cause is much more readable.

EDIT: a tutorial that would resolve the first case:

https://css-tricks.com/managing-wordpress-metadata-in-gutenberg-using-a-sidebar-plugin/

Related Posts:

  1. post formats – how to switch meta boxes when changing format?
  2. Adding an assisting editor box to Post page
  3. Metabox Data not being saved [closed]
  4. WordPress widget new instance creates content duplicates
  5. Options to get my custom post type metadata via the WordPress API
  6. Add box with custom per-page properties
  7. Adding a meta box to determine the sidebar [closed]
  8. add post meta front end edit
  9. Custom sidebars – get_sidebar() problems
  10. I can not display meta value in extras.php and template-tags.php
  11. Add post meta data date to event
  12. Check if any meta on the post has value then display content
  13. I created a Custom Meta Box but it is not displaying the value on my post page
  14. update_post_meta() throws Uncaught error: Cannot create duplicate attribute
  15. Remove action of an external plugin after checking if custom post meta is set
  16. How we insert values into database using metabox WordPress?
  17. Post meta box data not saving
  18. Filtering Tabs in a Custom Post Type’s Edit Page
  19. Hide individual page title using checkbox in custom meta box?
  20. How do I retrieve the slug of the current page?
  21. Most efficient way to get posts with postmeta
  22. Get posts by meta value
  23. Explanation of update_post_(meta/term)_cache
  24. How to extract data from a post meta serialized array?
  25. Passing error/warning messages from a meta box to “admin_notices”
  26. Add “upload media” button in meta box field
  27. How to save an array with one metakey in postmeta?
  28. WordPress is stripping escape backslashes from JSON strings in post_meta
  29. Get list of all registered sidebars
  30. List all sidebar names?
  31. How can I get the post ID from a WP_Query loop?
  32. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  33. What is the index [0] for on post meta fields?
  34. How to update_post_meta value as array
  35. Adding meta tag without plugin
  36. What’s the point of get_post_meta’s $single param?
  37. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  38. Individual Widgets per Page
  39. How to edit a post meta data in a Gutenberg Block?
  40. Sanitizing integer input for update_post_meta
  41. Execute action after post is saved with all related post_meta records (data)
  42. Lack of composite indexes for meta tables
  43. How can I retrieve multiple get_post_meta values efficiently?
  44. Get a single post by a unique meta value
  45. Add the sidebar/widget editor to the post edit screen?
  46. if get_post_meta is empty do something
  47. How we get_post_meta without post id
  48. How get post id from meta value
  49. What is the code to get the download link for a product in WooCommerce?
  50. Safe to delete blank postmeta?
  51. HowTo: Add Class to Sidebar Widget List-Items
  52. Custom field metabox not showing in back-end
  53. advanced custom fields update_field for field type: Taxonomy
  54. How to wrap the widget content with a div or get the widget title outside?
  55. update_post_meta not saving when value is zero
  56. Get sidebar parameters (before_widget, before_title, etc.) from within a widget
  57. Content hooks vs User hooks
  58. Meta compare with date (stored as string) not working
  59. How to hide meta box values from custom fields list?
  60. dynamic_sidebar not rendering sidebar
  61. Trying to get custom post meta through Jetpack JSON API [closed]
  62. How to update/insert custom field(post meta) data with wordpress REST API?
  63. get registered metaboxes by post type or post ID
  64. How do I remove all the metaboxes for a custom post type?
  65. Get custom side bar on custom post archive page
  66. Restrict post edit/delete based on user ID and custom field
  67. get_post_meta returning empty string when data shows in the database
  68. publish_post action hook doesn’t give post_meta_data
  69. Remove WordPress.org Meta link
  70. Remove post meta keys
  71. How to access the post meta of a post that has just been published?
  72. Why time functions show invalid time zone when using ‘c’ time format?
  73. How to get meta box data to display on a page
  74. Save metabox with multiple checkbox array
  75. esc before saving or before displaying does it matter?
  76. Why is get_post_meta returning an array when I specify it as single?
  77. How to batch update post content with custom post meta value
  78. Add div class to only one widget
  79. How to update/delete array in post meta value?
  80. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  81. Add a meta description to home page?
  82. Add multiple images to a page sidebar
  83. How to get the name and description of a sidebar in theme?
  84. Get all posts from custom post type and insert in select input as metabox
  85. Detect meta value changes when post is updated (post_updated)
  86. delete unused postmeta
  87. Should I sanitize custom post meta if it is going to be escaped later?
  88. How to avoid widgets added to sidebar on theme activation?
  89. Add post meta based on another post meta value before publish post
  90. Registering Sidebars and Sidebar Widgets. Sidebar Widgets Not Displaying
  91. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  92. Check if meta key value already exists
  93. Front-end update_post_meta snippet displays white screen?
  94. Query between two meta values?
  95. Save both current and new version of post meta
  96. Get Advanced Custom Fields values before saving [closed]
  97. Give extra post-meta to RSS feeds
  98. How to get meta value in wp_attachment_metadata
  99. WP REST API “rest_no_route” when trying to update meta
  100. Clean up output added via wp_head()
Categories post-meta Tags metabox, post-meta, register-sidebar, sidebar
How to remove private posts from RSS feeds?
Custom filter from post meta

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress