Skip to content

Instantly share code, notes, and snippets.

@mkaz
Last active December 23, 2022 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkaz/3b9c57330947d86a7dc9a7f41cc0cd20 to your computer and use it in GitHub Desktop.
Save mkaz/3b9c57330947d86a7dc9a7f41cc0cd20 to your computer and use it in GitHub Desktop.
const {__} = wp.i18n; // Import __() from wp.i18n
const {registerBlockType} = wp.blocks; // Import registerBlockType() from wp.blocks
const {
RichText
} = wp.blockEditor;
/**
* Register block
*/
export default registerBlockType(
"tct/article-card-block",
{
attributes: {
title: {
type: "string",
source: 'text',
selector: 'h2',
default: "untitled"
},
content: {
type: 'array',
source: 'children',
selector:'.content',
default: "..."
},
},
edit({attributes, setAttributes}) {
return (
<Fragment>
<RichText
tagName="h2"
multiline={false}
allowedFormats={[]}
value={attributes.title}
onChange={(title) => setAttributes({title})}
/>
<RichText
tagName="div"
multiline="p"
value={attributes.content}
onChange={(content) => setAttributes({content})}
/>
</Fragment>
);
},
save({attributes, className}) {
return (
<div className={className}>
<RichText.Content tagName="h2" value={attributes.title}/>
<RichText.Content className="content" tagName="div" value={attributes.content}/>
</div>
);
}
}
);
@Mahjouba91
Copy link

Amazing, thank you 🙏 , works like a charm now

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