How do I activate a certain block template only when editing the front page?

Setting $post_type_object->template seems to be done on ‘init’ (or close to it) while is_front_page() is set later, so I had to use $_GET[‘post’] instead. I also changed get_post_type_object( ‘post’ ) to ‘page’. Like this: add_action( ‘init’, ‘home_block_template’ ); function home_block_template() { if ( ! is_admin() || ! isset( $_GET[‘post’] ) || get_option( ‘page_on_front’ ) !== … Read more

How to import Reusable Blocks programatically?

The easiest solution so far seems to be creating the Reusable Blocks as custom post types: wp_insert_post([ ‘post_content’ => ‘<!– wp:shortcode –> [slider] <!– /wp:shortcode –>’, ‘post_title’ => ‘My Slider’, ‘post_type’ => ‘wp_block’, ‘post_status’ => ‘publish’, ‘comment_status’ => ‘closed’, ‘ping_status’ => ‘closed’, ‘guid’ => sprintf( ‘%s/wp_block/%s’, site_url(), sanitize_title(‘my-slider’) ) ]); This way, I can create … Read more

how can default_content filter tell if content will load in block editor or classic editor?

Looks like WP_Screen::is_block_editor() is what you want. Here’s some rough code to demonstrate: add_filter( ‘default_content’, function( $content, $post ) { $screen = function_exists( ‘get_current_screen’ ) ? get_current_screen() : null; $is_block_editor = $screen ? $screen->is_block_editor() : false; if ( $is_block_editor ) { // Do your block editor stuff here } else { // Do your classic … Read more

Override RenderCallback from LatestPosts

You certainly can do (not recommended). I just dig into the source there’s no filter/action hooks used in register_block_type() and it’s related classes. The only way (WordPress 5.3) is to redefine the latest post block again within your theme or plugin. The best way will be creating your custom blocks. // Remove the existing action … Read more

How to reload data stored for a sever side gutenberg block in the editor

First thing that jumps out is that in: <!– wp:mvc/block-mvc-offer-details {“data_offer”:”2426″} /–> the data_offer value is stored as a string while it’s defined attribute type is number. This could be why it’s stripped after reload. While select(‘core’).getEntityRecords does return IDs as integers it’s possible that after passing that data to <SelectControl> and then receiving the … Read more

Gutenberg – Remove add block button

You can register a template for your CPT and lock that template so users cannot add, move, or delete blocks. There are several ways to set up the template, but since your CPT is already registered, the easiest might be: <?php add_action( ‘init’, ‘wpse_360075_register_block_template’ ); function wpse_360075_register_block_template() { // Make sure to change “yourcptslug” to … Read more