Skip to content

Instantly share code, notes, and snippets.

@gh640
Last active July 4, 2024 03:14
Show Gist options
  • Save gh640/21cc5caf290397addb25a84840436508 to your computer and use it in GitHub Desktop.
Save gh640/21cc5caf290397addb25a84840436508 to your computer and use it in GitHub Desktop.
WordPress: Usage of Block Bindings API for custom fields
<!-- wp:paragraph {
"metadata":{
"bindings":{
"content":{
"source":"core/post-meta",
"args":{
"key":"book-author"
}
}
}
}
} -->
<p></p>
<!-- /wp:paragraph --
<!-- wp:paragraph {
"metadata":{
"bindings":{
"content":{
"source":"core/post-meta",
"args":{
"key":"book-genre"
}
}
}
}
} -->
<p></p>
<!-- /wp:paragraph -->
<?php
add_action( 'init', function () {
register_meta(
'post',
'book-genre',
[
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'Default text field',
]
);
register_meta(
'post',
'book-author',
[
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'Default text field',
]
);
} );
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"100%"} -->
<div class="wp-block-column" style="flex-basis:100%"><!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"book-genre"}},"__default":{"source":"core/pattern-overrides"}},"name":"Genre"}} -->
<p></p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"book-author"}},"__default":{"source":"core/pattern-overrides"}},"name":"Author"}} -->
<p></p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
@gh640
Copy link
Author

gh640 commented Jul 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment