Blocks
Creating block types
<?php
namespace HivePress\Blocks;
use HivePress\Helpers as hp;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
class Foo_Bar extends Block {
// Declare the block properties.
protected $say_hello;
public function __construct( $args = [] ) {
// Set the property defaults.
$args = hp\merge_arrays(
[
'say_hello' => false,
],
$args
);
parent::__construct( $args );
}
public static function init( $meta = [] ) {
// Add label and settings for Gutenberg.
$meta = hp\merge_arrays(
[
'label' => 'Hello World',
'settings' => [
'say_hello' => [
'label' => 'Say Hello',
'type' => 'checkbox',
'_order' => 123,
],
],
],
$meta
);
parent::init( $meta );
}
protected function boot() {
// Do something after the block is loaded.
parent::boot();
}
public function render() {
// Render the block HTML content.
$output = '<h1>';
if ( $this->say_hello ) {
$output .= 'Hello World!';
} else {
$output .= 'Silence...';
}
$output .= '</h1>';
return $output;
}
}
Customizing block types
Last updated