Skip to content

Instantly share code, notes, and snippets.

@simonhlee97
Last active January 12, 2024 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonhlee97/dce945304590aaf1bb68f5eb13a81550 to your computer and use it in GitHub Desktop.
Save simonhlee97/dce945304590aaf1bb68f5eb13a81550 to your computer and use it in GitHub Desktop.
WP-custom-block-plugin-starter
<?php
/*
Plugin Name: Awesome Block Block Type
Version: 1.0
Author: Simon
Author URI: https://github.com/simonhlee97
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class AwesomeBlock {
function __construct() {
add_action('init', [$this, 'onInit']);
}
function onInit() {
wp_register_script('awesomeBlockScript', plugin_dir_url(__FILE__) . 'build/index.js', array('wp-blocks', 'wp-i18n', 'wp-editor'));
wp_register_style('awesomeBlockStyle', plugin_dir_url(__FILE__) . 'build/index.css');
register_block_type('ourplugin/awesome-block', array(
'render_callback' => [$this, 'renderCallback'],
'editor_script' => 'awesomeBlockScript',
'editor_style' => 'awesomeBlockStyle'
));
}
function renderCallback($attributes) {
return '<p>We will replace this content soon.</p>';
}
}
$awesomeBlock = new AwesomeBlock();
// this file needs to be inside a folder "src", beside the index.scss file.
import "./index.scss"
wp.blocks.registerBlockType("ourplugin/awesome-block", {
title: "Awesome Block",
description: "A custom block type built with React",
icon: "welcome-learn-more",
category: "common",
edit: EditComponent,
save: function () {
return null
}
})
function EditComponent(props) {
return (
<div className="awesome-block-wrapper">
<div className="awesome-block-select-container">
We will have a select dropdown form element here.
</div>
<div>
The HTML preview of the selected professor will appear here.
</div>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment