Edit the my-custom-block.php using an IDE and add the following code:\n<?php\n\/*\nPlugin Name: My Custom Block\n*\/<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/span>Step 2: Load The Block Editor Scripts<\/span><\/h3>\nThe next step is to load the block editor scripts, enabling the block editor to recognize the custom block’s scripts when loading the website. Here’s how you can load the block editor scripts:<\/p>\n
\n- In the my-custom-block.php file, add the following code:\n
add_action('enqueue_block_editor_assets', 'load_my_custom_block_assets');\nfunction load_my_custom_block_assets() {\nwp_enqueue_script(\n'my-custom-block',\nplugin_dir_url( __FILE__ ) . 'dist\/block.js',\n[ 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor' ],\nfilemtime( plugin_dir_path( __FILE__ ) . 'dist\/block.js' )\n);\n}<\/code><\/pre>\n<\/li>\n- In the same file, add this code to load the block’s styles:\n
add_action('wp_enqueue_scripts', 'load_my_custom_block_styles');\nfunction load_my_custom_block_styles() {\nwp_enqueue_style(\n'my-custom-block-style',\nplugin_dir_url( __FILE__ ) . 'dist\/block.css',\n[],\nplugin_dir_path( __FILE__ )\n);\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/span>Step 3: Register The Custom Block<\/span><\/h3>\nThis step involves adding information to let WordPress know about the block, such as the name of the block, the category in which it falls, and the block’s attributes. Here’s how you can register the custom block:<\/p>\n
\n- In the my-custom-block.php file, add the following code:\n
add_action('init', 'register_my_custom_blocks');\nfunction register_my_custom_blocks() {\nregister_block_type(\n'my-custom-block\/my-custom-block',\n[\n'attributes' => [\n'textColor' => [\n'type' => 'string',\n'default' => '#000000',\n],\n'backgroundColor' => [\n'type' => 'string',\n'default' => '#ffffff',\n],\n],\n'editor_script' => 'my-custom-block',\n'editor_style' => 'my-custom-block-style',\n'style' => 'my-custom-block-style',\n'render_callback' => 'render_my_custom_block',\n]\n);\n}<\/code><\/pre>\n<\/li>\n<\/ol>\nThe above code uses the register_block_type() function, which accepts two parameters; the unique name of the block and an array of settings that include attributes, scripts, and styles. The attributes array defines the block’s attributes used to capture user input, while editor_script and editor_style tell WordPress which scripts and styles to use in the block editor. The style property is used to specify styles that apply to the front-end of the website, while the render_callback function is responsible for rendering the custom block.<\/p>\n
<\/span>Step 4: Create The Block Template<\/span><\/h3>\nThe final step is to create the block template. This is where website owners can design the custom block’s appearance, functionality, and layout. Here’s how you can create the block template:<\/p>\n
\n- In the my-custom-block directory, create a new directory named “src” and create a block.js file within the src directory.<\/li>\n
- Add the following code to load the block editor components:\n
import { RichText, InspectorControls } from '@wordpress\/block-editor';\nimport { PanelBody, PanelRow, TextControl } from '@wordpress\/components';\nimport { addFilter } from '@wordpress\/hooks';\nimport { __ } from '@wordpress\/i18n';\n\nfunction my_custom_block_options( settings, name ) {\nif ( name !== 'core\/group' ) {\nreturn settings;\n}\n\nsettings.attributes = Object.assign( settings.attributes, {\ntextColor: {\ntype: 'string',\ndefault: '#000000'\n},\nbackgroundColor: {\ntype: 'string',\ndefault: '#ffffff'\n},\n} );\n\nreturn settings;\n}\naddFilter( 'blocks.registerBlockType', 'my_custom_block_options', my_custom_block_options );<\/code><\/pre>\n<\/li>\n- Create a “dist” directory within the “my-custom-block” directory and create a block.js and block.css file within the “dist” directory.<\/li>\n
- Add the following code to design the custom block’s appearance, functionality, and layout:\n
const edit = ( props ) => {\nconst { className, setAttributes, attributes } = props;\nconst { textColor, backgroundColor } = attributes;\n\nconst handleChangeTextColor = ( newTextColor ) => {\nsetAttributes( { textColor: newTextColor } );\n};\n\nconst handleChangeBackgroundColor = ( newBackgroundColor ) => {\nsetAttributes( { backgroundColor: newBackgroundColor } );\n};\n\nreturn (\n<div\nclassName={ className }\nstyle={ {\ncolor: textColor,\nbackgroundColor: backgroundColor,\n} }\n>\n<InspectorControls>\n<PanelBody title={ __( 'Color Settings' ) }>\n<PanelRow>\n<TextControl\nlabel={ __( 'Text Color' ) }\nvalue={ textColor }\nonChange={ handleChangeTextColor }\n\/>\n<\/PanelRow>\n<PanelRow>\n<TextControl\nlabel={ __( 'Background Color' ) }\nvalue={ backgroundColor }\nonChange={ handleChangeBackgroundColor }\n\/>\n<\/PanelRow>\n<\/PanelBody>\n<\/InspectorControls>\n<RichText\ntagName=\"p\"\nclassName=\"my-custom-block-text\"\nstyle={ { color: textColor } }\nvalue={ attributes.content }\nonChange={ ( content ) => setAttributes( { content } ) }\nplaceholder={ __( 'Enter your text here...' ) }\n\/>\n<\/div>\n);\n};\n\nconst save = ( props ) => {\nconst { className, attributes } = props;\nconst { textColor, backgroundColor, content } = attributes;\n\nreturn (\n<div\nclassName={ className }\nstyle={\n{\ncolor: textColor,\nbackgroundColor: backgroundColor,\n} }\n>\n<RichText.Content\ntagName=\"p\"\nclassName=\"my-custom-block-text\"\nstyle={ { color: textColor } }\nvalue={ content }\n\/>\n<\/div>\n);\n};<\/code><\/pre>\n<\/li>\n<\/ol>\nThe above code uses InspectorControls, PanelBody, PanelRow, TextControl, and RichText to customize the custom blocks’ appearance, functionality, and layout in the editor. The save function renders the custom block on the front-end of the website.<\/p>\n
<\/span>Conclusion<\/span><\/h2>\nIn conclusion, registering a custom block in WordPress may seem intimidating at first, but it’s not as complicated as it sounds. Follow the mentioned steps to register the custom block. Remember to create a plugin, load the block editor scripts, register the custom block, and create the block template. With this knowledge in mind, website owners can design their own custom blocks and improve their website’s overall functionality and user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"
Understanding WordPress: Registering Custom Blocks If you’re looking to create a unique website, then WordPress is the perfect platform for you. With its open-source content…<\/p>\n","protected":false},"author":1,"featured_media":7209,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3287],"tags":[],"jetpack_featured_media_url":"https:\/\/mauricemuteti.info\/wp-content\/uploads\/2023\/03\/WORDPRESS-ULTIMATE-GUIDE-How-To-step-by-step-Tutorial.png","_links":{"self":[{"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/posts\/8433"}],"collection":[{"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/comments?post=8433"}],"version-history":[{"count":0,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/posts\/8433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/media\/7209"}],"wp:attachment":[{"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/media?parent=8433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/categories?post=8433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mauricemuteti.info\/wp-json\/wp\/v2\/tags?post=8433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}