PHP code example of magonxesp / block-autoload

1. Go to this page and download the library: Download magonxesp/block-autoload 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/ */

    

magonxesp / block-autoload example snippets


     

// example/Example.php
namespace YourThemeNamespace\Blocks;

use MagonxESP\BlockAutoload\Annotation\Block;
use MagonxESP\BlockAutoload\Block\BlockBase;

/**
 * Class Example
 * 
 * @Block(
 *     name="example",
 *     title="Example",
 *     description="Example block",
 *     icon="",
 *     domain="my-site",
 *     category="custom",
 *     keywords={"keyword1", "keyword2"},
 *     template="example.template.php"
 * )
 */
class Example extends BlockBase {
    
    public $hello;

    public function setup() {
        $this->hello = 'Hello wordpress';
    }
}

// functions.php
use MagonxESP\BlockAutoload\BlockAutoload;
use MagonxESP\BlockAutoload\Block\BlockPlugin;
    
add_action('init', function() {
    // autoload blocks registering them using the ACF PRO Block API (Require ACF PRO plugin installed and activated)
    $block_autoloader = new BlockAutoload(BlockPlugin::ACF_PRO, __DIR__ . '/blocks');
    $block_autoloader->setBlockNamespace('YourThemeNamespace\\Blocks\\');
    $block_autoloader->load();
});
shell script
$ cd blocks
$ mkdir example
$ touch example/Example.php # the block class
$ touch example/example.template.php # the block template

<!-- example/example.template.php -->
<h1> echo $context['hello'];