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-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-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-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-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-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-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-17.js
Created January 30, 2019 12:23
➊➐ Get a list of registered taxonomy objects
const { select } = wp.data;
const { getTaxonomies } = select( 'core' );
const taxonomies = getTaxonomies();
@httpstersk
httpstersk / gutenberg-snippets-18.js
Created January 31, 2019 20:15
➊➑ Retrieve the details of a specific post
const POST_ID = 25;
const { select } = wp.data;
const { getEntityRecords } = select( 'core' );
const [ postDetails ] = getEntityRecords( 'postType', 'post', { include: POST_ID } );
@httpstersk
httpstersk / gutenberg-snippets-19.js
Created February 3, 2019 13:47
➊➒ Get the maximum upload size allowed
const { select } = wp.data;
const { getEditorSettings } = select( 'core/editor' );
const { maxUploadFileSize } = getEditorSettings();
const maxMB = Number( maxUploadFileSize / Math.pow( 1024, 2 ) );
@httpstersk
httpstersk / gutenberg-snippets-20.js
Created February 4, 2019 17:36
➋×➓ Check if a logged-in user can upload files
const { select } = wp.data;
const { hasUploadPermissions } = select( 'core' );
const canUpload = hasUploadPermissions();