-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a tutorial for creating a meta block #12834
Conversation
I think it's worth adding a PHP render callback for the block to show people how to get the meta data in a block. |
docs/designers-developers/developers/tutorials/metabox/add-meta-block.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/add-meta-block.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/add-meta-block.md
Outdated
Show resolved
Hide resolved
8198db1
to
f4a99d7
Compare
@swissspidy Do you mean for using meta data in a different block? I do want to show a way to use the data somewhere else, but I have not been able to come up with good example case. |
@swissspidy I added an extra step showing how to use the post meta both in a block and in a filter. Take a look and let me know if that addresses your concern. Thanks! |
So, does one have to create a new separate block for every different meta field? On custom post types with many meta fields we'll end up creating many very similar blocks with the only difference being the meta key used. |
No, a block can contain an infinite number of such fields. |
@mattyrob if you can share your code that you have I can try to help troubleshoot, and update the tutorial to fix any bumpy spots. You can either post here in the comments or if a Github Gist is easier, anyway that works for you. I've walked through and tested again and the code is working for me, but then I made it so that tends to happen. :-) |
Thanks for reaching out, I think it got it pretty much working in the end - the 2 things that need clarification for me are this (feel free to correct if I'm wrong). 1/ When making the
2/ In my plugin I load specific hooks on the admin side or the frontend, documentation I read says that REST API hooks need to go on the frontend, this doesn't work well in the new editor (Gutenberg) as the data fields are not accessible in admin (if I get change I'll try to find that documentation - it may need updating). |
```php | ||
// register custom meta tag field | ||
function myguten_register_meta() { | ||
register_meta( 'post', 'myguten_meta_block_field', array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not register_post_meta
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find register_post_meta
to be slightly more complicated since the syntax requires passing an empty string in for the post type to cover all types.
Whereas using register_meta()
and passing in post
covers all post types by default.
I don't feel that strongly about it, so could switch easily.
Only if your meta field is protected. If you look at |
Will you add the ES6 version of the following code? |
In f346699 I updated to include a section regarding keys starting with underscore and how to work with that. |
@mrleemon I do not plan to include an ES6 version, ES5 works without the build step and is easier to explain the concepts with a simple change/reload and not worrying about builds running/breaking and other issues unrelated to the core of the tutorial. If you prefer ES6 syntax, converting should be straight-forward and a good exercise to confirm you understand the material. |
Most docs in the handbook have both ES5 and ES6 code examples, don't they? |
f346699
to
84a929b
Compare
84a929b
to
57d2d92
Compare
Rebased from master to update TOC and Manifest. Pushed some changes to update links, move the image to the assets folder and update the meta field name in the examples. |
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md
Outdated
Show resolved
Hide resolved
d9aefef
to
2122d84
Compare
…ock-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com>
Co-Authored-By: mkaz <marcus@mkaz.com>
Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com>
docs/designers-developers/developers/tutorials/metabox/readme.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few small tweaks; more TK
docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final set of tweaks. Love this tutorial; nice work @mkaz!
docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md
Outdated
Show resolved
Hide resolved
docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md
Outdated
Show resolved
Hide resolved
…ock-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com>
…ock-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com>
Thanks @chrisvanpatten and @nosolosw I've incorporated all the feedback, I appreciate the assist on making this a great tutorial. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Props @mkaz this is great!
I'm loving these tutorials. Thanks. |
* Add a tutorial for creating a meta block * Update docs/designers-developers/developers/tutorials/metabox/add-meta-block.md Co-Authored-By: mkaz <marcus@mkaz.com> * Improving wording * Batch of Edits and updates * Rebase * Fix headers * Switch out to use TextControl components * Simplify and edits * minor updates * Reorganize tutorial, add example usage * Address @swisspidy comment and add example using render_callback * Remove create plugin part and link to JavaScript tutorial, so we are not duplicating content, allows for updating in one spot if things change. * Change filenames to match flow, easier to edit and follow * Fix link * Add meta block tutorial to TOC, Manifest and Tutorials Links * Fix whitespace * Add section regarding protected key * Simplify links * Update per feedback, clarification around PHP and JS files * Update output, clarify PHP code. Add check for value exists * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Fix newlines * Update docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com> * Add link to DevHub * Remove newline
* Add a tutorial for creating a meta block * Update docs/designers-developers/developers/tutorials/metabox/add-meta-block.md Co-Authored-By: mkaz <marcus@mkaz.com> * Improving wording * Batch of Edits and updates * Rebase * Fix headers * Switch out to use TextControl components * Simplify and edits * minor updates * Reorganize tutorial, add example usage * Address @swisspidy comment and add example using render_callback * Remove create plugin part and link to JavaScript tutorial, so we are not duplicating content, allows for updating in one spot if things change. * Change filenames to match flow, easier to edit and follow * Fix link * Add meta block tutorial to TOC, Manifest and Tutorials Links * Fix whitespace * Add section regarding protected key * Simplify links * Update per feedback, clarification around PHP and JS files * Update output, clarify PHP code. Add check for value exists * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/readme.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-4-use-data.md Co-Authored-By: mkaz <marcus@mkaz.com> * Fix newlines * Update docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-1-intro.md Co-Authored-By: mkaz <marcus@mkaz.com> * Update docs/designers-developers/developers/tutorials/metabox/meta-block-2-register-meta.md Co-Authored-By: mkaz <marcus@mkaz.com> * Add link to DevHub * Remove newline
Description
This tutorial walks a developer through create a meta block, which is a block that stores data to post meta. It includes showing how to use a template to auto include the meta block so it is not hidden.
Fixes: #12621 #7696
Preview at: https://github.com/WordPress/gutenberg/tree/docs/metabox/tutorial/docs/designers-developers/developers/tutorials/metabox
Types of changes
Documentation.