GithubHelp home page GithubHelp logo

isabella232 / plugin-with-blocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sayhellogmbh/plugin-with-blocks

0.0 0.0 0.0 8 KB

The code in this repository is intended as a copy-paste resource for adding custom blocks to the WordPress block editor using the block.json registration technique.

JavaScript 68.44% PHP 30.97% SCSS 0.59%

plugin-with-blocks's Introduction

Plugin with blocks

The code in this repository is intended as a copy-paste resource for adding custom blocks to the WordPress block editor using the block.json registration technique.

This code should be copied into a plugin, according to the standardized file structure in place for Say Hello Plugins and Themes for WordPress.

The general base of such a Plugin is at https://github.com/WPSwitzerland/plugin-boilerplate-psr.

The general base of such a Theme is at https://github.com/sayhellogmbh/hello-fse.

Placeholder in this code

The placeholders PLUGIN_NAME and PLUGIN_NAME_PASCAL_CASE appear throughout this code. Replace them with the actual plugin slugs -- e.g. shp_myfirstblock or shp_clientblocks -- and the pascal-cased versions of the name -- e.g. ShpMyfirstblock -- before starting work.

## Adding a new block

Each block should receive its own subfolder within the src folder, as per the contents of the example MyFirstBlock folder.

The block.json file contains the configuration for each block, which WordPress uses in order to register the block.

The block registration happens through the register_block_type call in the Block package, which gets called on the init hook. The example uses server-side rendering, which takes place through the renderBlock method in the Block package.

The individual Block packages are loaded from the loadClasses method in the Plugin class. The base code contains an autoloader which will find the appropriate files. Add an entry for each block in this loader.

e.g.: Blocks\MyFirstBlock\Block::class, maps to src/Blocks/MyFirstBlock/Block.php.

Use the namespace namespace SayHello\PLUGIN_NAME_PASCAL_CASE\Blocks\MyFirstBlock; in the block files.

Saving JSX-generated content

If you want to render the content directly in the editor using JSX, and save this content to the database directly, add the save method to the src/Blocks/MyFirstBlock/assets/src/scripts/editor/index.js file.

In this case, remove the render_callback from the Block package.

Assets

The block.json file defines which scripts and style files are to be loaded. The editor JavaScript file is essential: without this, the block cannot appear in the block editor.

Compilation

The SCSS files and the Javascript files are compiled by a Gulp process, with Webpack compilation for the JavaScript files. This is essential, so that the JavaScript source files can be written using JSX. The compiled files (.css and .js) are loaded from the assets/dist folder. The source files are within the assets/src folder.

The Gulp task files are in the .build/gulp folder. Thes differ from the regular task files in the Hello FSE Theme, but largely follow the same basic premise. The tasks -- in particular the script task -- is specifically customised for use with this file structure.

In order to start the compilation watch process, run npm install and then npm start. This will start the watcher, which will re-compile the available files when they are saved.

Asset loading

The files from e.g. src/MyFirstBlock/assets/src/styles will be saved to src/MyFirstBlock/assets/dist/styles. The dist path should be used in the block.json file, thus: "editorScript": "file:./assets/dist/scripts/editor.js".

Each block has its own block.json, src and dist files. Depending on the configuration, these files may be merged by WordPress core or loaded inline. In some cases, the frontend assets might only be loaded if the block is placed in the content of the page being viewed.

This code does not need to load any assets: that is taken care of by WordPress core.

Gitignore

The dist folder is excluded from this repository by a rule in the .gitignore file. They must not be excluded from a plugin using this code base!

Author

Mark Howells-Mead, Say Hello GmbH [email protected]

Since 9.9.2022

plugin-with-blocks's People

Contributors

markhowellsmead avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.