name: gutenberg-block
description: Create a new Gutenberg block for the Saman SEO plugin. Use when adding editor blocks for schema markup, SEO elements, or content enhancement tools.
argument-hint: [block-name] [description]
Create Gutenberg Block
Generate a new Gutenberg block for the Saman SEO plugin.
Arguments
$ARGUMENTS should contain: block name (e.g., "review") and description
Steps
-
Create block directory at blocks/{block-name}/
-
Create block.json (block metadata):
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "saman-seo/{block-name}",
"version": "1.0.0",
"title": "{Block Title}",
"category": "saman-seo",
"icon": "{dashicon-name}",
"description": "{Block description}",
"keywords": ["{keyword1}", "{keyword2}", "seo"],
"supports": {
"html": false,
"align": ["wide", "full"]
},
"textdomain": "saman-seo",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:./render.php",
"attributes": {
"exampleAttribute": {
"type": "string",
"default": ""
}
}
}
- Create index.js (editor script):
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import metadata from './block.json';
import './editor.scss';
const Edit = ({ attributes, setAttributes }) => {
const blockProps = useBlockProps();
const { exampleAttribute } = attributes;
return (
<>
<InspectorControls>
<PanelBody title={__('Settings', 'saman-seo')}>
<TextControl
label={__('Example Setting', 'saman-seo')}
value={exampleAttribute}
onChange={(value) => setAttributes({ exampleAttribute: value })}
/>
</PanelBody>
</InspectorControls>
<div {...blockProps}>
{/* Block editor preview */}
<div className="saman-seo-{block-name}">
{/* Content */}
</div>
</div>
</>
);
};
const Save = () => null;
registerBlockType(metadata.name, {
edit: Edit,
save: Save,
});
- Create render.php (server-side render):
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$wrapper_attributes = get_block_wrapper_attributes( array(
'class' => 'saman-seo-{block-name}',
) );
$schema = array(
'@context' => 'https://schema.org',
'@type' => '{SchemaType}',
);
?>
<div <?php echo $wrapper_attributes;
<!-- Block HTML output -->
</div>
<?php if ( ! empty( $schema ) ) : ?>
<script type="application/ld+json">
<?php echo wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); ?>
</script>
<?php endif; ?>
- Create editor.scss (editor styles):
.wp-block-saman-seo-{block-name} {
padding: 20px;
border: 1px solid #ddd;
border-radius: 4px;
background: #f9f9f9;
}
- Create style.scss (frontend styles):
.saman-seo-{block-name} {
margin: 1em 0;
padding: 1em;
}
- Register the block in PHP (if not using block.json auto-registration):
Add to includes/class-saman-seo-plugin.php or create a dedicated service:
add_action( 'init', function() {
register_block_type( SAMAN_SEO_PATH . 'blocks/{block-name}' );
} );
- Update build configuration in
package.json if needed
Existing Block Patterns
Reference existing blocks in blocks/:
blocks/breadcrumbs/ - Navigation breadcrumbs with schema
blocks/faq/ - FAQ schema block
blocks/howto/ - How-To schema block
Schema Types to Consider
Review / AggregateRating
Product
Event
Recipe
Article / NewsArticle
Person
Organization
LocalBusiness
Best Practices
- Use block.json for metadata (WordPress 5.8+)
- Dynamic rendering - Use
render.php for server-side output
- Schema validation - Ensure JSON-LD is valid
- Accessibility - Include proper ARIA attributes
- Responsive - Test on mobile viewports
- i18n - Wrap all strings for translation
Example Usage
/gutenberg-block review Star rating and review schema block for products and services