Last active
March 31, 2020 03:53
-
-
Save bshelling/ee6a1336222fa82a533b69768112256a to your computer and use it in GitHub Desktop.
Create a Custom Gutenberg Block in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a Custom Gutenberg Block in Wordpress | |
// Author: bshelling | |
// Link: | |
import { registerBlockType } from '@wordpress/blocks'; | |
const homeBlock = <div className="homepage-section-block"><h1 className="title">Headline</h1><p className="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce in dui sapien. Donec tincidunt diam in libero porttitor, quis feugiat diam mollis. | |
</p><button>Read More</button></div>; | |
registerBlockType('bshelling/hm-section-block',{ | |
title: 'Homepage Section Block', | |
description: 'The copy section is for use on the homepage', | |
icon: 'text', | |
category: 'layout', | |
edit: function(){ | |
return (homeBlock); | |
}, | |
save: function(){ | |
return (homeBlock); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Homepage Section Block | |
* Plugin URI: https://myhomepagesection.example | |
* Description: Homepage sections for Wordpress | |
* Version: 1.0 | |
*/ | |
function hmSectionBlock(){ | |
$buildAssets = include(plugin_dir_path(__FILE__)).'build/index.asset.php'; | |
/** | |
* Editor Script | |
*/ | |
wp_register_script('hmsb-editor-script',plugins_url('build/index.js',__FILE__), $buildAssets['dependencies'],$buildAssets['version']); | |
/** | |
* Editor Style | |
*/ | |
wp_register_style('hmsb-editor-style',plugins_url('build/css/style.css',__FILE__),'',$buildAssets['version'] | |
); | |
/** | |
* Register block | |
*/ | |
register_block_type('bshelling/hm-section-block',array('editor_script' => 'hmsb-editor-script','editor_style'=>'hmsb-editor-style')); | |
} | |
add_action('init','hmSectionBlock'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "hmsection-block", | |
"version": "1.0.0", | |
"description": "Homepack section block", | |
"main": "index.js", | |
"author": "bshelling", | |
"license": "MIT", | |
"scripts": { | |
"build": "wp-scripts build && node-sass --include-path sass src/sass/style.scss -o build/css", | |
"dev:watch": "wp-scripts start && node-sass --include-path sass src/sass/style.scss -o build/css -w" | |
}, | |
"devDependencies": { | |
"@wordpress/scripts": "^7.1.3", | |
"node-sass": "^4.13.1", | |
}, | |
"dependencies": { | |
"@wordpress/blocks": "^6.12.3", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment