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-3.js
Created January 9, 2019 08:13
➌ Change the excerpt of the current post
const { dispatch } = wp.data;
const { editPost } = dispatch( 'core/editor' );
const excerpt = 'Gutenberg Editor 🙌';
editPost( { excerpt } );
@httpstersk
httpstersk / gutenberg-snippets-2.js
Last active January 9, 2019 08:13
➋ Parse the slug from the permalink (returns null if the post is not viewable)
const { select } = wp.data;
const { getPermalinkParts } = select( 'core/editor' );
const slug = getPermalinkParts().postName;
console.log(slug);
@httpstersk
httpstersk / gutenberg-snippets-4.js
Created January 10, 2019 08:07
➍ Get the list of all categories
const { select } = wp.data;
const { getEntityRecords } = select( 'core' );
const MAX = 100;
const categories = getEntityRecords( 'taxonomy', 'category', { per_page: MAX });
@httpstersk
httpstersk / gutenberg-snippets-5.js
Created January 11, 2019 08:10
➎ Get the url of the current featured image
const { select } = wp.data;
const { getMedia } = select( 'core' );
const { getEditedPostAttribute } = select( 'core/editor' );
const mediaId = getEditedPostAttribute('featured_media');
const mediaUrl = getMedia(mediaId).source_url;
console.log(mediaUrl);
@httpstersk
httpstersk / gutenberg-snippets-6.js
Created January 12, 2019 18:52
➏ Make the post `sticky`
const { dispatch } = wp.data;
const { editPost } = dispatch( 'core/editor' );
editPost( { sticky: true } );
@httpstersk
httpstersk / gutenberg-snippets-7.js
Last active January 13, 2019 09:21
➐ Toggle full–screen mode
const { dispatch, select } = wp.data;
const { isFeatureActive } = select( 'core/edit-post' )
const { toggleFeature } = dispatch( 'core/edit-post' );
if ( isFeatureActive( 'fullscreenMode' ) ) {
toggleFeature( 'fullscreenMode' );
}
@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
@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-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' ];