-
-
Save waqashassan98/99d9f9bdea395034594a50631877d872 to your computer and use it in GitHub Desktop.
Example of how to use registerBlockType() to create a custom block in WordPress https://wp.zacgordon.com/2017/12/28/how-to-use-to-registerblocktype-to-create-blocks-in-wordpress/
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
// Get helper functions from global scope | |
const { registerBlockType } = window.wp.blocks; | |
const { __ } = window.wp.i18n; | |
// Use registerBlockType to create a custom block | |
registerBlockType( | |
'example-plugin/example-custom-block', | |
{ | |
// Localize title using wp.i18n.__() | |
title: __( 'Block Title' ), | |
// Category Options: common, formatting, layout, widgets, embed | |
category: 'common', | |
// Dashicons Options - https://goo.gl/aTM1DQ | |
icon: 'wordpress-alt', | |
// Limit to 3 Keywords / Phrases | |
keywords: [ | |
__( 'Example' ), | |
__( 'Project' ), | |
__( 'Code Samples' ) | |
], | |
// Attributes set for each piece of dynamic data used in your block | |
attributes: { | |
exampleContent: { | |
type: 'array', | |
source: 'children', | |
selector: 'div.my-content', | |
}, | |
}, | |
// Determines what is displayed in the editor | |
edit: props => { | |
const onChangeContent = value => { | |
props.setAttributes( { exampleContent: value } ); | |
}; | |
return ( | |
<div className={props.className}> | |
<Editable | |
tagname="div" | |
multiline="p" | |
className="my-content" | |
placeholder={ __( 'Add your content...' ) } | |
value={props.attributes.exampleContent} | |
onChange={onChangeContent} | |
/> | |
</div> | |
); | |
}, | |
// Determines what is displayed on the frontend | |
save: props => { | |
return ( | |
<div className={props.className}> | |
{props.attributes.exampleContent} | |
</div> | |
); | |
}, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment