Introducing Post Format Block

Post Formats are a somewhat neglected feature in WordPress. How neglected? Well as I discovered, there isn’t a block for it in Full Site Editing So I decided to build it and thankfully it is super easy. The @wordpress/create-block package got the majority of my scaffold for me in minutes, after that it just a little bit of JavaScript and a little bit of PHP to power my new plugin.

// Block Registration Function
import { registerBlockVariation } from '@wordpress/blocks';

// Internationalization
import { __ } from '@wordpress/i18n';

// Icon
import { postCategories as icon } from '@wordpress/icons';

// https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/post-terms/variations.js
const variation = {
        name: 'post_format',
        title: __('Post Format'),
        description: __("Display a post's format"),
        icon,
        isDefault: true,
        attributes: { term: 'post_format' },
        isActive: (blockAttributes) => blockAttributes.term === 'post_format',
};

registerBlockVariation( 'core/post-terms', variation );

Block Variations make it so that I don’t need to build a block from scratch, and the core/post-terms block is already designed for this. The Post Tags and Category blocks are both variations on the post terms block.

On the server side, I just needed to register my block and make sure that post formats are always available for the Rest API.

/**
 * Register our block
 *
 * The block type we register is actually a variant, but this allows it to show up as a "block" plugin.
 */
function jorbin_post_format_block_block_init() {
        register_block_type( __DIR__ );
}

add_action( 'init', 'jorbin_post_format_block_block_init' );

/**
 * Make the post formats avilable in the REST API
 */
function jorbin_post_format_block_taxonomy_args( $args, $taxonomy_name ) {
    if ( 'post_format' === $taxonomy_name ) {
        $args['show_in_rest'] = true;
        }
        return $args;
}
add_filter( 'register_taxonomy_args', 'jorbin_post_format_block_taxonomy_args', 10, 2 );

This is my first new plugin since BRAD and my first ever block-based plugin. If you also love Post Formats, give it a try!

You can download the plugin from the WordPress Plugin Repository. Additionally, pull requests and bug reports are welcome to https://github.com/aaronjorbin/post-format-block, but I will likely consider this complete software and don’t intend to add any new features.

GET AARON IN YOUR INBOX


Comments

2 responses to “Introducing Post Format Block”

  1. This Article was mentioned on aaron.jorb.in

  2. […] בלוק post format, וסקירה של זה ב-WPTavern – אחד ממפתחי וורדפרס המובילים, Aaron Jorbin, החליט שאין מספיק התייחסות ל-post formats ולכן פיתח בלוק כזה. ושרה גודינד מ–WPTavern סקרה אותו. […]