Hire Now!
Whether you’re looking to launch your brand, showcase your portfolio, or open an online store, we’re here to bring your ideas to life.
The Gutenberg editor, introduced in WordPress 5.0, revolutionized how content is created and managed. Its block-based structure allows users to design complex layouts with ease. While the editor includes a variety of default blocks, creating custom Gutenberg blocks enables you to meet specific design and functionality requirements.
This guide will walk you through the process of creating custom Gutenberg blocks for your WordPress site.
Gutenberg blocks are modular elements that represent different types of content, such as paragraphs, images, or galleries. Custom blocks let you extend this functionality to include unique components tailored to your needs.
wp-content/plugins/
.custom-gutenberg-blocks
.1- Inside your folder, create a file named custom-gutenberg-blocks.php
.
2- Add the following header:
<?php /* Plugin Name: Custom Gutenberg Blocks Description: A plugin to create custom Gutenberg blocks. Version: 1.0 Author: Your Name */ ?>
3- Activate the plugin from the WordPress dashboard.
Create a block.js
file in your plugin folder to register the block.
In your custom-gutenberg-blocks.php
file, enqueue the block scripts:
function custom_gutenberg_block_scripts() {
wp_enqueue_script(
'custom-block-js',
plugins_url('block.js', __FILE__),
array('wp-blocks', 'wp-element', 'wp-editor'),
'1.0',
true
);
}
add_action('enqueue_block_editor_assets', 'custom_gutenberg_block_scripts');
block.js
const { registerBlockType } = wp.blocks;
const { RichText } = wp.blockEditor;
registerBlockType('custom/block', {
title: 'Custom Block',
icon: 'smiley',
category: 'common',
attributes: {
content: { type: 'string', source: 'html', selector: 'p' }
},
edit: ({ attributes, setAttributes }) => {
const onChangeContent = (content) => {
setAttributes({ content });
};
return (
<RichText
tagName="p"
value={attributes.content}
onChange={onChangeContent}
placeholder="Enter text here..."
/>
);
},
save: ({ attributes }) => {
return <RichText.Content tagName="p" value={attributes.content} />;
}
});
Add a style.css
file to your plugin folder for frontend styles:
.custom-block {
background-color: #f9f9f9;
padding: 20px;
border: 1px solid #ddd;
}
Enqueue your CSS file in custom-gutenberg-blocks.php
:
function custom_gutenberg_block_styles() {
wp_enqueue_style(
'custom-block-css',
plugins_url('style.css', __FILE__),
array(),
'1.0'
);
}
add_action('enqueue_block_assets', 'custom_gutenberg_block_styles');
Custom Gutenberg blocks enable you to create tailored content elements that enhance your WordPress site’s functionality and design. By following this guide, you can build blocks that meet your unique requirements and provide an improved editing experience.
Start creating your custom Gutenberg blocks today to unlock the full potential of the WordPress editor.
Website security is a top priority for WordPress site owners. From brute force attacks to malware, threats are every…
Affiliate marketing is a proven way to monetize your website and drive additional revenue streams. WordPress affilia…
Integrating secure and efficient payment gateways is essential for any e-commerce site. WordPress payment gateway pl…
WooCommerce is one of the most popular platforms for building online stores, but its functionality can be significan…
No results available
ResetDiscussion
Get the latest news about our updates and discounts
Copyright © 2024 - CollectWP
Powered by DeoPixel with love & passion 💪
Content Harmony: On-Site Data