Hook: editor_blocks
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Hook: editor_blocks

The editor_blocks hook allows plugins to register custom blocks for the block editor. Each block is defined with its file path, title, and category to enable dynamic loading into the editor.

Usage


$pluginBlocks = DynamicHooks::executeHook('editor_blocks', true);
if (!empty($pluginBlocks)) {
    foreach ($pluginBlocks as $pluginName => $blocks) {
        if (!is_array($blocks)) {
            error_log("Invalid blocks returned by plugin: $pluginName");
            continue;
        }
        if (is_array($blocks)) {
            foreach ($blocks as $block) {
                if (!isset($block['file'], $block['title'], $block['category'])) {
                    error_log("Invalid block structure in plugin: $pluginName");
                }
                if (isset($block['file'], $block['title'], $block['category'])) {
                    $categories[$block['category']][] = [
                        'file' => $block['file'],
                        'title' => $block['title']
                    ];
                }
            }
        }
    }
}
    

This hook processes blocks registered by plugins and categorizes them dynamically for the editor.

Example Implementation


DynamicHooks::addHook('editor_blocks', function () {
    return [
        'your_plugin' => [
            [
                'file' => 'plugins/your_plugin/frontend/blocks/faq_question_block.php', 
                'title' => 'Your Plugin Question Block', 
                'category' => 'custom_'
            ],
            [
                'file' => 'plugins/your_plugin/frontend/blocks/faq_list_block.php', 
                'title' => 'Your Plugin List Block', 
                'category' => 'custom_'
            ]
        ]
    ];
}, 'your_plugin');
    

Best Practices

  • Ensure blocks include valid file, title, and category properties.
  • Use descriptive titles and logical categories to organize blocks effectively.
  • Test blocks thoroughly to verify correct loading and functionality.

Troubleshooting

  • Verify that the hook is correctly registered under the intended namespace.
  • Check for missing or invalid block properties in the returned array.
  • Inspect logs for errors during block loading or categorization.
Was this article helpful?
0 out of 0 found this helpful