Last active
February 10, 2018 19:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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