Skip to content

Instantly share code, notes, and snippets.

@mklasen
Created May 8, 2019 08:46
Show Gist options
  • Save mklasen/8ad5187d0e69426493231987178ea7ce to your computer and use it in GitHub Desktop.
Save mklasen/8ad5187d0e69426493231987178ea7ce to your computer and use it in GitHub Desktop.
Simple ACF Gutenberg Block
<?php
/* Plugin Name: ACF 5.8 Test
*/
class ACF_Test_Block {
public $_name = 'test-acf-block';
public function __construct() {
add_action( 'acf/init', [ $this, 'register_block' ] );
add_action( 'acf/init', [ $this, 'register_fields' ] );
}
public function register_block() {
// register a testimonial block.
acf_register_block_type(
array(
'name' => $this->_name,
'title' => __( 'Test ACF Block' ),
'description' => __( 'Test ACF Block' ),
'render_callback' => [ $this, 'render_block' ],
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'acf', 'test' ),
'enqueue_style' => plugin_dir_url(__FILE__) . '/test-field.css',
)
);
}
public function register_fields() {
acf_add_local_field_group(
array(
'key' => 'test_settings',
'title' => 'Test Settings',
'fields' => array(
array(
'key' => 'test_field',
'label' => 'Enter some text here',
'name' => 'test_field_text',
'type' => 'text',
),
array(
'key' => 'test_image',
'label' => 'Add an image',
'name' => 'test_field_image',
'type' => 'image',
'return_format' => 'id',
),
),
'location' => array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'acf/' . $this->_name,
),
),
),
)
);
}
public function render_block() {
$image_id = get_field( 'test_field_image' );
echo '<div class="'.$this->_name.'">';
if (false !== $image_id) {
echo wp_get_attachment_image($image_id);
}
echo '<h5>'.get_field( 'test_field_text' ) . '</h5>';
echo '</div>';
}
}
new ACF_Test_Block();
.test-acf-block {
display: flex;
}
.test-acf-block img {
margin-right: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment