Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coder618/952b287df83672efc2056932a56638b4 to your computer and use it in GitHub Desktop.
Save coder618/952b287df83672efc2056932a56638b4 to your computer and use it in GitHub Desktop.
Gutenberg Sample Block form carbon field wordpress plugin
<?php
use Carbon_Fields\Container;
use Carbon_Fields\Field;
use Carbon_Fields\Block;
add_action( 'carbon_fields_register_fields', 'dd' );
function dd(){
// var_dump( WP_Block_Type_Registry::get_instance()->get_all_registered() );
Block::make( __( 'My Shiny Gutenberg Block' ) )
->add_fields( array(
Field::make( 'text', 'heading', __( 'Block Heading' ) ),
Field::make( 'image', 'image', __( 'Block Image' ) ),
Field::make( 'rich_text', 'content', __( 'Block Content' ) ),
Field::make( 'radio', 'crb_radio', __( 'Choose Option' ) )->set_options( array(
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
) ),
Field::make( 'complex', 'crb_service_types' )
->add_fields( array(
Field::make( 'text', 'name', 'Repeater title' ),
Field::make( 'image', 'ri', 'Repeater image' )
))
))
->set_icon( 'heart' )
->set_keywords( [ __( 'lol' ) ] )
->set_description( __( 'A simple block consisting of a heading, an image and a text content.' ) )
->set_category( 'layout' )
->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
print_r($fields);
?>
<div class="block">
<div class="block__heading">
<h1><?php echo esc_html( $fields['heading'] ); ?></h1>
</div><!-- /.block__heading -->
<div class="block__image">
<?php echo wp_get_attachment_image( $fields['image'], 'full' ); ?>
</div><!-- /.block__image -->
<div class="block__content">
<?php echo apply_filters( 'the_content', $fields['content'] ); ?>
</div><!-- /.block__content -->
</div><!-- /.block -->
<?php
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment