ralfhortt/wp-block

ServerSideRender blocks 的 composer 包装器

1.1 2020-05-25 09:48 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:41 UTC


README

ServerSideRender blocks 的 composer 包装器

安装

$ composer require ralfhortt/wp-block

使用

  • 创建一个扩展 RalfHortt\WPBlock\Block 的自定义类
  • 设置 $name 属性
  • 设置 $attributes 属性
  • 添加一个 render 方法

示例

<?php
use RalfHortt\WPBlock\Block;

class MyBlock extends Block {
	protected $name = 'ralfhortt/myblock';
	protected $attributes = [
		'postType' => [
			'type' => 'string',
			'default' => '',
		],
		// …
	];

	protected function render($atts, $content): void
	{
		$query = new WP_Query([
			'post_type' => $atts['postType'],
			'showposts' => 5,
		]);

		if ( $query->have_posts()) {
			while ( $query->have_posts() ) {
				$query->the_post();
				the_title();
			}
		}

		wp_reset_query();
	}
}

class MyOtherBlock extends Block {
	protected string $name = 'ralfhortt/myotherblock';
	protected string $title = 'My other Block';
	protected string $blockJson = 'block.json';
	// …
}

钩子

操作

  • {$name}/before - 在块输出之前
  • {$name}/after - 在块输出之后

过滤器

  • {$name}/name - 块名称
  • {$name}/attributes - 块属性
  • {$name}/render - 覆盖渲染回调

更新日志

v1.2 - 2022-05-10

  • 添加对 block.json 的支持

v1.1 - 2020-05-25

  • 应用 StyleCI 的修复

v1.0 - 2020-01-17

  • 首次发布