Giter Site home page Giter Site logo

block-worskshop's Introduction

How To Make WordPress Blocks

The steps below are over-simplifed. This code goes with an in person workshop.

Download Slides

Introduction

This repo has the end result of setting up the repo, adding a static block and then making it editable. The steps below walk you through that process.

Requirments

  • Your computer should have:
  • Optional:
  • git
  • Docker

Installing (Optional)

If you want to use this as is:

  • Clone repo:
    • git clone https://github.com/Shelob9/block-worskshop
    • You should clone into the plugin directory of your local site OR into an empty directory -- if you wish to use included docker-compose file for local development.
  • Swith to the plugin's directory and install dependencies.
    • cd block-workshop
    • npm install
  • IF using provided local environement:
    • docker-compose up -d

Write Code! Make Blocks

Create Your Block Plugin

  • Create package.json:
    • npm init
    • Answer questions. Don't forget to use GPL-2.0 or GPL-3.0 license.
  • Install WordPress scripts
    • npm i @wordpress/scripts
  • Update "scripts" in package.json
  • Ignore directories in .gitingore:
node_modules/
vendor/
build/

Setup Files

  • Create src/index.js
  • Put some JavaScript in there.
  • Compile it to make sure everything works.
    • npm run build
    • You should see built JavaScript in build/
  • Be impressed by how simple webpack and Babel are to use :)

Register With WordPress

Create Your Block

  • Open src/index.js
  • Write Some JavaScript:
    • Import dependencies
    • registerBlockType()
    • Edit Callback
    • Save Callback
  • For manual testing while developing
    • npm run start
  • To ship for production:
    • npm run build

Make Block Editable

  • Install new dependencies:
    • npm i @wordpress/editor @wordpress/components
  • Import TextControl from @wordpress/components
    • Use for editting message
  • Import InspectorControls from @wordpress/editor
  • Add message attribute
  • Get current value of message from attributes prop passed to save and edit.
  • Create update function for message using setAttributes prop passed to edit callback.
  • Display current value of message in edit and save callbacks.
  • Use TextControl to created edit interface for block.
  • Wrap the control in InsepctorControls so it goes in the inspector controls.

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.