Skip to content

Instantly share code, notes, and snippets.

@ryelle
Last active October 7, 2020 21:09
Show Gist options
  • Save ryelle/cacdba5c888153d97eae38db775dec7f to your computer and use it in GitHub Desktop.
Save ryelle/cacdba5c888153d97eae38db775dec7f to your computer and use it in GitHub Desktop.
Example container block
<?php
/**
* Plugin Name: Internationalization Block
* Description: This is some demo for i18n support.
* Version: 0.1.0
* Author: ryelle
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: ryelle-i18n
*
* @package ryelle-i18n
*/
namespace Ryelle\I18N_Block;
/**
* Break the world.
*/
function register_block() {
$dir = dirname( __FILE__ );
wp_register_script(
'ryelle-i18n-i18n-demo-block-editor',
plugins_url( 'index.js', __FILE__ ),
array('wp-block-editor', 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-polyfill'),
time()
);
wp_set_script_translations( 'ryelle-i18n-i18n-demo-block-editor', 'ryelle-i18n' );
wp_register_style(
'ryelle-i18n-i18n-demo-block',
plugins_url( 'style.css', __FILE__ ),
array(),
filemtime( __DIR__ . '/style.css' )
);
register_block_type( 'ryelle-i18n/i18n-demo', array(
'editor_script' => 'ryelle-i18n-i18n-demo-block-editor',
'style' => 'ryelle-i18n-i18n-demo-block',
) );
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Restrict allowed blocks on all post types, for testing.
*/
add_filter(
'allowed_block_types',
function( $allowed_block_types ) {
return array(
'core/paragraph',
// This plugin block.
'ryelle-i18n/i18n-demo',
// Some blocks from wordpress.org.
'book-review-block/book-review',
'a8c/starscape'
);
}
);
/**
* WordPress dependencies
*/
const { __ } = wp.i18n;
const { InnerBlocks } = wp.blockEditor;
const { registerBlockType } = wp.blocks;
const { createElement } = wp.element;
/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType("ryelle-i18n/i18n-demo", {
title: __("Internationalization Block", "ryelle-i18n"),
description: __("This is some demo for i18n support.", "ryelle-i18n"),
category: "common",
icon: "admin-site-alt2",
supports: {
html: false,
},
edit: ({ className }) => {
/* Core blocks, plus one block-directory block */
const ALLOWED_BLOCKS = ["core/image", "core/heading", "core/paragraph", "book-review-block/book-review"];
/* Create a template from core blocks. */
const TEMPLATE = [
[
"core/heading",
{ level: 2, placeholder: __("Your Title", "ryelle-i18n") },
],
];
return createElement(
"div",
{ className: className },
createElement(InnerBlocks, {
allowedBlocks: ALLOWED_BLOCKS,
template: TEMPLATE,
template_lock: false,
})
);
},
save: () => {
return createElement(
"div",
{},
createElement(InnerBlocks.Content)
);
},
});
.wp-block-ryelle-i18n-i18n-demo {
background-color:var(--wp-admin-theme-color);
color:#fff;
padding:2px
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment