Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to needed to build a WP block, at least the php portion. Good way to convert WP Shortcodes
<?php
defined( 'ABSPATH' ) || die( 'File cannot be accessed directly' );
function built_with_php_init() {
// Register our block editor script.
wp_enqueue_script(
'built-with-php',
plugins_url( 'block.js', __FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' )
);
register_block_type(
'wclancpa-2019/built-with-php',
array(
'attributes' =>
array(
'value_one' => array(
'type' => 'string',
'default' => 'A simple text box',
),
'value_two' => array(
'type' => 'string',
'default' => 'Ain\'t this purdy?',
),
'value_three' => array(
'type' => 'string',
'default' => 'This will be more of a description.... And I could go on and on...',
),
),
// The script name we gave in the wp_register_script() call.
'editor_script' => 'built-with-php',
'render_callback' => 'built_with_php_render',
)
);
add_shortcode( 'built-with-php-shortcode', 'built_with_php_render' );
}
add_action( 'init', 'built_with_php_init' );
function built_with_php_render( $attributes ) {
$return = '<h2 style="color:salmon;">' . ( print_r( $attributes['value_one'], true ) ?: 'value_one not defined' ) . '</h2>';
$return .= '<h4>' . print_r( $attributes['value_two'], true ) . '</h4>';
$return .= '<p>' . print_r( $attributes['value_three'], true ) . '</p>';
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.