Skip to content

Instantly share code, notes, and snippets.

View httpstersk's full-sized avatar

Lubos Belak httpstersk

View GitHub Profile
@httpstersk
httpstersk / gutenberg-snippets-16.js
Created January 26, 2019 16:44
➊➏ Create an admin notice with custom styled HTML element
const NOTICE_STATUS = 'success'; // Available: info, success, warning, error
const HTML_ELEMENT = '<h1 style="text-align: center">Gutenberg ist gut! 🙌</h1>';
const { dispatch } = wp.data;
const { createNotice } = dispatch( 'core/notices' );
createNotice( NOTICE_STATUS, HTML_ELEMENT, { __unstableHTML: true } );
@httpstersk
httpstersk / gutenberg-snippets-15.js
Created January 25, 2019 18:01
➊➎ Create an admin notice with custom `info` message
const { dispatch } = wp.data;
const { createInfoNotice } = dispatch( 'core/notices' );
createInfoNotice( 'Gutenberg ist gut!' );
@httpstersk
httpstersk / gutenberg-snippets-14.js
Created January 23, 2019 14:32
➊➍ Add a new custom taxonomy field
const { dispatch } = wp.data;
const { saveEntityRecord } = dispatch( 'core' );
const TAXONOMY_NAME = 'custom';
saveEntityRecord( 'taxonomy', TAXONOMY_NAME, { name: 'X', slug: 'x' } );
@httpstersk
httpstersk / gutenberg-snippets-13.js
Created January 22, 2019 16:01
➊➌ Add a new post with pre-defined title (saved as a draft)
const { dispatch } = wp.data;
const { saveEntityRecord } = dispatch( 'core' );
const newPost = { title: 'Guten Tag, Gutenberg!' };
saveEntityRecord( 'postType', 'post', newPost );
@httpstersk
httpstersk / gutenberg-snippets-12.js
Created January 21, 2019 16:14
➊➋ Get the content of the post being edited in raw string format
const { select } = wp.data;
const { getEditedPostContent } = select( 'core/editor' );
const rawPostContent = getEditedPostContent();
@httpstersk
httpstersk / gutenberg-snippets-11.js
Created January 17, 2019 17:09
➊➊ Get all block objects for the current post
const { select } = wp.data;
const { getBlocks } = select( 'core/editor' );
const blocks = getBlocks();
@httpstersk
httpstersk / gutenberg-snippets-10.js
Created January 16, 2019 13:59
➓ Set & get a custom post meta
const { dispatch, select } = wp.data;
const { editPost } = dispatch( 'core/editor' );
const { getEditedPostAttribute } = select( 'core/editor' );
editPost({ meta: { is_special: true } })
const isSpecial = getEditedPostAttribute( 'meta' )[ 'is_special' ];
@httpstersk
httpstersk / gutenberg-snippets-10.js
Created January 16, 2019 13:59
➓ Set & get a custom post meta
const { dispatch, select } = wp.data;
const { editPost } = dispatch( 'core/editor' );
const { getEditedPostAttribute } = select( 'core/editor' );
editPost({ meta: { is_special: true } })
const isSpecial = getEditedPostAttribute( 'meta' )[ 'is_special' ];
@httpstersk
httpstersk / gutenberg-snippets-9.js
Last active January 15, 2019 14:25
➒ Get the list of post tags
const { select } = wp.data;
const { getEntityRecords } = select( 'core' );
const tags = getEntityRecords( 'taxonomy', 'post_tag', { orderby: 'count', order: 'desc' });
@httpstersk
httpstersk / gutenberg-snippets-8.js
Last active January 14, 2019 14:47
➑ Get the avatars of all the WordPress users (`admin` or `editor` roles only)
const { select } = wp.data;
const { getAuthors } = select( 'core' );
const authors = getAuthors();
const avatars = authors.map(a => a.avatar_urls[ '96' ]); // Available Sizes: 24/48/96