Skip to content
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

Merged
merged 35 commits into from
Jan 11, 2019
Merged

Add a tutorial for creating a meta block #12834

merged 35 commits into from
Jan 11, 2019

Conversation

mkaz
Copy link
Member

@mkaz mkaz commented Dec 13, 2018

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.

@mkaz mkaz added the [Type] Developer Documentation Documentation for developers label Dec 13, 2018
@mkaz mkaz self-assigned this Dec 13, 2018
@swissspidy
Copy link
Member

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.

@mkaz mkaz added the [Status] In Progress Tracking issues with work in progress label Dec 14, 2018
@mkaz
Copy link
Member Author

mkaz commented Dec 19, 2018

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.

@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.

@mkaz
Copy link
Member Author

mkaz commented Dec 19, 2018

@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!

@mkaz mkaz removed the [Status] In Progress Tracking issues with work in progress label Dec 19, 2018
@mrleemon
Copy link
Contributor

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.

@swissspidy
Copy link
Member

No, a block can contain an infinite number of such fields.

@mkaz
Copy link
Member Author

mkaz commented Jan 2, 2019

@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. :-)

@mattyrob
Copy link

mattyrob commented Jan 3, 2019

@mkaz

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 register_meta() function call and 'auth_callback' parameter needs including otherwise the editor user doesn't have permission to update the meta data field.
I'm using:

auth_callback' => function() {
	return current_user_can( 'edit_posts' );
},

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(
Copy link
Member

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?

Copy link
Member Author

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.

@swissspidy
Copy link
Member

When making the register_meta() function call and 'auth_callback' parameter needs including otherwise the editor user doesn't have permission to update the meta data field.

Only if your meta field is protected.

If you look at register_meta(), you can see that auth_callback defaults to __return_true unless the meta key is protected (starts with an undersccore), in which case it's __return_false` and you need to pass your own callback.

@mrleemon
Copy link
Contributor

mrleemon commented Jan 3, 2019

@mkaz
Copy link
Member Author

mkaz commented Jan 3, 2019

In f346699 I updated to include a section regarding keys starting with underscore and how to work with that.

@mkaz
Copy link
Member Author

mkaz commented Jan 3, 2019

@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.

@swissspidy
Copy link
Member

Most docs in the handbook have both ES5 and ES6 code examples, don't they?

@oandregal
Copy link
Member

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.

nosolosw and others added 5 commits January 11, 2019 06:14
…ock-4-use-data.md

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>
Copy link
Contributor

@chrisvanpatten chrisvanpatten left a 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

Copy link
Contributor

@chrisvanpatten chrisvanpatten left a 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!

chrisvanpatten and others added 12 commits January 11, 2019 07:06
…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>
@mkaz
Copy link
Member Author

mkaz commented Jan 11, 2019

Thanks @chrisvanpatten and @nosolosw I've incorporated all the feedback, I appreciate the assist on making this a great tutorial.

Copy link
Contributor

@chrisvanpatten chrisvanpatten left a 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!

@mkaz mkaz merged commit 64b5837 into master Jan 11, 2019
@mkaz mkaz deleted the docs/metabox/tutorial branch January 11, 2019 17:22
@youknowriad
Copy link
Contributor

I'm loving these tutorials. Thanks.

@youknowriad youknowriad added this to the Documentation & Handbook milestone Jan 18, 2019
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
* 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
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants