PHPackages                             kuserich/wp-block-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kuserich/wp-block-api

ActiveLibrary

kuserich/wp-block-api
=====================

Small collection of helpful interfaces and classes for the development of WordPress Blocks and Extensions.

v1.0.4(2y ago)15GPL-3.0PHPPHP &gt;=8.1

Since Sep 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kuserich/wp-block-api)[ Packagist](https://packagist.org/packages/kuserich/wp-block-api)[ Docs](https://www.goeckeritz.xyz)[ RSS](/packages/kuserich-wp-block-api/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (7)Used By (0)

WordPress Block API
===================

[](#wordpress-block-api)

Small collection of helpful interfaces and abstract classes to make the development of WordPress Blocks and Extensions easier and more consistent.

Requirements
------------

[](#requirements)

- PHP version 7.3 or greater
- WordPress version 5.7 or greater

Installation
------------

[](#installation)

You will need [Composer](https://getcomposer.org/) installed on your computer in order to build and use this package.

- Add this package to the `require` field in your `composer.json`
- Import `vendor/autoload.php` in your project

```
composer require kuserich/wp-block-api
```

---

Usage
=====

[](#usage)

In Blocks &amp; Extensions
--------------------------

[](#in-blocks--extensions)

Simply extend the block or extension class that fits the requirements of your package and extend all relevant interfaces (for extensions):

```
namespace Kuserich;

final class My_Block extends Block {

	public static function register(): void {
		register_block_type_from_metadata( plugin_dir_path( __DIR__ ) );
	}

}
```

```
namespace Kuserich;

final class My_Extension extends Extension {

	public static function register(): void {
		Functions::register_extension_from_metadata( plugin_dir_path( __DIR__ ) );
	}

}
```

Development and usage is simplified if all blocks and extensions use `namespace Kuserich`.

### Create an `extension.json`

[](#create-an-extensionjson)

Registration of extensions is done using a JSON configuration file akin to the `block.json`file that WordPress Core uses.

Example:

```
{
	"name": "wp-extension-awesome-feature",
	"frontendScript": "file:./build/script.js",
	"frontendStyle": "file:./build/style.css",
	"script": "file:./build/both.js",
	"style": "file:./build/style-index.css",
	"editorScript": "file:./build/index.js",
	"editorStyle": "file:./build/index.css",
	"requires": [
		"kuserich/add-to-cart"
	]
}
```

Currently, `extension.json` uses the following fields:

#### name

[](#name)

Defines the name of the extension. This field is heavily utilized for asset handles and must be unique.

#### frontendScript

[](#frontendscript)

File handle used for the script that's only enqueued in the frontend. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### frontendStyle

[](#frontendstyle)

File handle used for the style that's only enqueued in the frontend. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### script

[](#script)

File handle used for the script that's enqueued in the editor and the frontend. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### style

[](#style)

File handle used for the style that's enqueued in the editor and the frontend. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### editorScript

[](#editorscript)

File handle used for the script that's only enqueued in the editor. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### editorStyle

[](#editorstyle)

File handle used for the style that's only enqueued in the editor. Use `file:` prefix if you are passing a path to a local file. The path must be relative to `extension.json`.

#### requires

[](#requires)

An array of block names that the given extension requires. During asset enqueueing, the post content is check if at least one of the passed blocks is present. If it is not, the extension assets are not enqueued to improve performance.

In Projects
-----------

[](#in-projects)

Each block and extension includes an `init` function that can be called to initialize the class. No other function needs to be called.

### As a Plugin

[](#as-a-plugin)

To run your block or extension as a standalone plugin, simply create a plugin file (e.g. `index.php`) that calls the `init` function of your class.

```
