Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Last active April 27, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbrocks/432a1ff35c32239fbaf6d88f1c90b480 to your computer and use it in GitHub Desktop.
Save pbrocks/432a1ff35c32239fbaf6d88f1c90b480 to your computer and use it in GitHub Desktop.
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