Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active February 10, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/d0cdd75ee6479691e92f993ee1470842 to your computer and use it in GitHub Desktop.
Save Shelob9/d0cdd75ee6479691e92f993ee1470842 to your computer and use it in GitHub Desktop.
Example code for https://github.com/WordPress/gutenberg/issues/4989 - Me attempting to save attributes as post content and post meta. Work around is here: https://gist.github.com/Shelob9/36d7096d919995ae615d4d4bbf10ffbc
(function (wp) {
var el = wp.element.createElement;
var __ = wp.i18n.__;
var TextControl = wp.blocks.InspectorControls.TextControl;
wp.blocks.registerBlockType('shelob9/metatest', {
title: __('Meta and Markup', 'metatest'),
category: 'common',
edit: function (props) {
return el(
'div',
{},
[
el(
'p',
{className: props.className},
props.attributes.content
),
el(
TextControl,
{
label: __('Content', 'metatest'),
value: props.attributes.content,
onChange: function (value) {
props.setAttributes({content: value});
}
},
)
]
)
},
save: function (props) {
return el(
'p',
{className: props.className},
props.attributes.content
);
}
});
})(
window.wp
);
<?php
function metatest_enqueue_block_editor_assets() {
$dir = dirname( __FILE__ );
$block_js = 'metatest/block.js';
$editor_css = 'metatest/editor.css';
wp_enqueue_script( 'metatest-block', plugins_url( $block_js, __FILE__ ), array(
'wp-blocks',
'wp-i18n',
'wp-element',
), filemtime( "$dir/$block_js" ) );
wp_enqueue_style( 'metatest-block', plugins_url( $editor_css, __FILE__ ), array(
'wp-blocks',
), filemtime( "$dir/$editor_css" ) );
}
add_action( 'enqueue_block_editor_assets', 'metatest_enqueue_block_editor_assets' );
add_action( 'init', function(){
register_block_type( 'shelob9/metatest', array(
'attributes' => array(
'content' => array(
'type' => 'string',
'source' => 'meta',
'meta' => 'metatest_content'
)
)
));
register_meta( 'post', 'metatest_content', array(
'show_in_rest' => true
));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment