1. Go to this page and download the library: Download codelight/acf-blocks 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/ */
codelight / acf-blocks example snippets
// blocks.php
use Codelight\ACFBlocks\Blocks;
use Codelight\ACFBlocks\BlockType;
add_action('init', function() {
// Create a new block
$imageBlock = new BlockType('example_image');
/**
* Define ACF fields - see https://github.com/StoutLogic/acf-builder
* Add an image field and a wysiwyg field.
* Also add the field group to template-image.php page template
*/
$imageBlock->getFieldsBuilder()
->addImage('awesome_image', ['return_format' => 'id'])
->addWysiwyg('boring_text')
->setLocation('page_template', '==', 'template-image.php');
/**
* Add a function for processing raw ACF data before it's sent to the template.
* This allows you to do additional processing depending on the data and keep your templates clean.
*/
$imageBlock->addCallback(function($data) {
// Return the full image html with srcset attribute generated by wordpress
$data['image'] = wp_get_attachment_image($data['awesome_image'], 'large');
// Split the wysiwyg contents into an excerpt and the full text
$data['excerpt'] = wp_trim_words($data['boring_text'], 25);
$data['text'] = $data['boring_text'];
return $data;
});
// Set the template for this block
$imageBlock->setTemplate('templates/blocks/image.php');
// Register the block with the main block manager class
$blocks = Blocks::getInstance();
$blocks->registerBlock($imageBlock);
}
// template-image.php
/**
* Template name: Image
*
* This is the page template where we will be rendering our block.
* Calling `blocks()->get()` inside the Loop will return all pre-rendered blocks
* assigned to that specific page or post.
*/
// templates/blocks/image.php
/**
* This is the template of the single block.
* Data is injected automatically.
*/
use Codelight\ACFBlocks\BlockType;
class ImageBlock extends BlockType {
protected $config = [
// The machine-readable name of the block
'name' => 'example_image';
// The location of the template
'template' => 'templates/blocks/image.php',
];
// This function is called when the block type is initialized for the first time.
// You'll use it mostly to register the fields
public function init()
{
$this->getFieldsBuilder()
->addImage('awesome_image', ['return_format' => 'id'])
->addWysiwyg('boring_text')
->setLocation('page_template', '==', 'template-image.php');
}
// This function works in a similar way to addCallback() - it allows you to
// modify the data that's passed into the template
public function filterData($data)
{
// Return the full image html with srcset attribute generated by wordpress
$data['image'] = wp_get_attachment_image($data['awesome_image'], 'large');
// Split the wysiwyg contents into an excerpt and the full text
$data['excerpt'] = wp_trim_words($data['boring_text'], 25);
$data['text'] = $data['boring_text'];
return $data;
}
}
action('init', function() {
$blocks = Blocks::getInstance();
$blocks->init([
'blocktypes' => [
// array of block class names as strings
'ImageBlock',
]
]);
});
use Codelight\ACFBlocks\FlexibleContentBlockType;
class FlexibleBlock extends FlexibleContentBlockType
{
protected $config = [
// The machine-readable name of the block
'name' => 'flexible_block',
// The location of the template
'template' => 'blocks.content-builder',
];
public function init()
{
$this->getFieldsBuilder()
->setGroupConfig('title', 'Content Blocks')
->setLocation('post_type', '==', 'page');
// This registers our ImageBlock as a child block of this flexible content layout
$this->registerBlockType('ImageBlock');
}
}
re_once('blocks/FlexibleBlock.php');
add_action('init', function() {
$blocks = Blocks::getInstance();
$blocks->init([
'blocktypes' => [
// array of block class names as strings
'ImageBlock',
'FlexibleBlock',
]
]);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.