Skip to content

Instantly share code, notes, and snippets.

@mario-neuhold
Last active January 10, 2019 17:50
Show Gist options
  • Save mario-neuhold/313ba94a4cf48b835c043d0b1b7ee4fa to your computer and use it in GitHub Desktop.
Save mario-neuhold/313ba94a4cf48b835c043d0b1b7ee4fa to your computer and use it in GitHub Desktop.
ACF Block example radio with default value
[
{
"key": "group_5c375da8e559d",
"title": "Block Radio choice with default value",
"fields": [
{
"key": "field_5c375e57e0abe",
"label": "Radio Choice",
"name": "radiochoice",
"type": "radio",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"1": "one",
"2": "two"
},
"allow_null": 0,
"other_choice": 0,
"default_value": 2,
"layout": "vertical",
"return_format": "value",
"save_other_choice": 0
}
],
"location": [
[
{
"param": "block",
"operator": "==",
"value": "acf\/radiodefault"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]
<?php
/**
* Block: Radio choice with default value
* Block slug: acf/radiodefault
*
* @see https://www.advancedcustomfields.com/resources/acf_register_block/
*/
global $post;
?>
<div class="block-radiodefault block-<?php echo $block['id'] ?>">
<?php echo "<pre>"; ?>
<?php // outputs int(2) in the block editor preview, NULL in the frontend ?>
<?php var_dump(get_field('radiochoice')); ?>
<?php echo "</pre>"; ?>
</div>
<?php
// register acf/radiodefault block.
acf_register_block_type( array(
'name' => 'test',
'title' => 'Radio Default',
'description' => 'Radio choice with default value',
'render_callback' => 'render_acf_block',
'category' => 'common',
'icon' => 'star-filled',
'keywords' => array( 'acf', 'radio' ),
) );
// render block template
function render_acf_block( $block ) {
// convert name ("acf/radiodefault") into path friendly slug ("radiodefault")
$slug = str_replace( 'acf/', '', $block['name'] );
// include a template part from within the "templates" folder
if( file_exists( STYLESHEETPATH . "/templates/block-{$slug}.php" ) ) {
include( STYLESHEETPATH . "/templates/block-{$slug}.php" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment