Last active
February 4, 2024 20:38
-
-
Save Shelob9/144055408101e2fdfc4bf34adc85dd04 to your computer and use it in GitHub Desktop.
Example Gutenberg block with server-side rendering. Gutenberg edit() block creates interface. Gutenberg saves settings automatically, the PHP function passed as `render_callback` to `register_block_type` is used to create HTML for front-end rendering of block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { __ } = wp.i18n; | |
const { registerBlockType } = wp.blocks; | |
const el = wp.element.createElement; | |
registerBlockType( 'hiRoy/serverSide', { | |
title: __( 'Server Side Block', 'text-domain' ), | |
icon: 'networking', | |
category: 'common', | |
attributes: { | |
images : { | |
default: [], | |
type: 'array', | |
} | |
}, | |
edit({attributes, setAttributes, className, focus, id}) { | |
//Put a user interface here. | |
}, | |
save({attributes, className}) { | |
//gutenberg will save attributes we can use in server-side callback | |
return null; | |
}, | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
register_block_type('hiRoy/serverSide', array( | |
'render_callback' => 'hi_roy_render_callback', | |
'attributes' => array( | |
'images' => array( | |
'type' => 'array' | |
) | |
) | |
) | |
); | |
function hi_roy_render_callback( $attributes ){ | |
$images = $attributes[ 'images' ]; | |
return '<div><!-- put image gallery here--></div>'; | |
} |
1ucay
commented
Dec 3, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment