Skip to content

Instantly share code, notes, and snippets.

@jurecuhalev
Forked from nicooprat/custom-callback.php
Last active February 22, 2022 13:34
Show Gist options
  • Save jurecuhalev/45ef79e5ca96dee3a66032eccc4c5130 to your computer and use it in GitHub Desktop.
Save jurecuhalev/45ef79e5ca96dee3a66032eccc4c5130 to your computer and use it in GitHub Desktop.
Create an ACF block with Sage 9 & Blade
add_action('acf/init', function() {
if( function_exists('acf_register_block') ) {
// Look into views/blocks
$dir = new \DirectoryIterator(\locate_template("views/blocks/"));
// Loop through found blocks
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$slug = str_replace('.blade.php', '', $fileinfo->getFilename());
// Get infos from file
$file_path = \locate_template("views/blocks/${slug}.blade.php");
$file_headers = get_file_data($file_path, [
'title' => 'Title',
'description' => 'Description',
'category' => 'Category',
'icon' => 'Icon',
'keywords' => 'Keywords',
]);
if( empty($file_headers['title']) ) {
die( _e('This block needs a title: ' . $file_path));
}
if( empty($file_headers['category']) ) {
die( _e('This block needs a category: ' . $file_path));
}
// Register a new block
$datas = [
'name' => $slug,
'title' => $file_headers['title'],
'description' => $file_headers['description'],
'category' => $file_headers['category'],
'icon' => $file_headers['icon'],
'keywords' => explode(' ', $file_headers['keywords']),
'render_callback' => 'my_acf_block_render_callback',
];
acf_register_block($datas);
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment