PHP code example of ralfhortt / wp-block

1. Go to this page and download the library: Download ralfhortt/wp-block library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ralfhortt / wp-block example snippets


use RalfHortt\WPBlock\BlockManifest;

(new BlockManifest(__DIR__ . '/build/blocks/blocks-manifest.php'))->register();

use RalfHortt\WPBlock\BlockDefaults;

// Static defaults
BlockDefaults::for('core/image')
    ->set('sizeSlug', 'large')
    ->set('linkDestination', 'media')
    ->register();

// Dynamic defaults with callbacks
BlockDefaults::for('core/image')
    ->set('sizeSlug', fn($metadata) => wp_is_mobile() ? 'thumbnail' : 'large')
    ->register();

// Multiple blocks with shared and individual defaults
BlockDefaults::for(['core/image', 'core/paragraph'])
    ->set('className', 'custom-block') // Both blocks
    ->set('core/image', 'sizeSlug', 'large') // Only core/image
    ->register();

use RalfHortt\WPBlock\BlockVariations;

(new BlockVariations([
    'core/image' => [
        [
            'name' => 'hero-image',
            'title' => __('Hero Image'),
            'attributes' => [
                'align' => 'wide',
                'className' => 'hero-image',
            ],
        ],
    ],
]))->register();

// Or use fluent interface
(new BlockVariations())
    ->addVariation('core/button', [
        'name' => 'cta-button',
        'title' => __('CTA Button'),
        'attributes' => ['className' => 'cta-button'],
    ])
    ->removeVariation('core/button', 'outline')
    ->register();

use RalfHortt\WPBlock\BlockStyles;

(new BlockStyles())
    ->removeStyle('core/button', 'outline')
    ->removeAllStyles('core/separator')
    ->register();

use RalfHortt\WPBlock\BlockSupports;

// Add supports
BlockSupports::for('core/image')
    ->add(['color' => true, 'alignment' => true])
    ->register();

// Add supports with nested configuration
BlockSupports::for('core/button')
    ->add([
        'color' => [
            'palette' => [
                ['name' => 'Red', 'slug' => 'red', 'color' => '#ff0000'],
            ],
        ],
    ])
    ->register();

// Remove supports
BlockSupports::for('core/image')
    ->remove(['spacing', 'padding', 'margin'])
    ->register();

// Combine add and remove
BlockSupports::for('core/paragraph')
    ->add(['color' => true, 'typography' => true])
    ->remove(['spacing'])
    ->register();

use RalfHortt\WPBlock\BlockManager;

$manager = new BlockManager('core/image');

// Configure supports
$manager->addSupports(['color' => true, 'alignment' => true]);
$manager->removeSupports(['spacing']);

// Configure styles (no need to repeat block name!)
$manager->removeStyle('outline');

// Configure variations (no need to repeat block name!)
$manager->addVariation([
    'name' => 'hero-image',
    'title' => 'Hero Image',
    'attributes' => ['align' => 'wide'],
]);

// Register all managers
$manager->register();

(new BlockManager('core/button'))
    ->addSupports(['color' => true])
    ->removeStyle('outline')
    ->addVariation(['name' => 'cta', 'title' => 'CTA Button'])
    ->register();

use RalfHortt\WPBlock\Block;

class MyBlock extends Block {
    protected string $name = 'myplugin/custom-block';
    protected string $title = 'My Custom Block';
    protected string $blockJson = 'block.json';
    
    public function render(array $attributes, string $content): string {
        return '<div class="my-block">' . esc_html($attributes['title'] ?? '') . '</div>';
    }
}

(new MyBlock())->register();