Skip to content

Instantly share code, notes, and snippets.

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 wpsmith/042eb240a0430eddb8b3d998c1a52cbc to your computer and use it in GitHub Desktop.
Save wpsmith/042eb240a0430eddb8b3d998c1a52cbc to your computer and use it in GitHub Desktop.
PHP: WordPress: How to add a classic block as a default.
<?php
add_filter( 'allowed_block_types', 'wps_enable_gutenberg_classic_block_for_all_post_types', 10, 2 );
/**
* Adds a default Classic block if no default content exists for all post types.
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param \WP_Post $post The post resource data.
*
* @return bool|array
*/
function wps_enable_gutenberg_classic_block_for_all_post_types( $allowed_block_types, $post ) {
// Add a classic block.
global $initial_edits;
$initial_edits = array(
'title' => $post->post_title,
'content' => $post->post_content ? $post->post_content : '<p>Add content here</p>',
'excerpt' => $post->post_excerpt,
);
// Return whatever allowed blocks was.
return $allowed_block_types;
}
<?php
add_filter( 'allowed_block_types', 'wps_enable_gutenberg_classic_block_for_one_post_type', 10, 2 );
/**
* Adds a default Classic block if no default content exists for one post type.
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param \WP_Post $post The post resource data.
*
* @return bool|array
*/
function wps_enable_gutenberg_classic_block_for_one_post_type( $allowed_block_types, $post ) {
// If not our post type, bail.
// Change my_post_type to post to enable only for posts.
if ( 'my_post_type' !== $post->post_type ) {
return $allowed_block_types;
}
// Add a classic block.
global $initial_edits;
$initial_edits = array(
'title' => $post->post_title,
'content' => $post->post_content ? $post->post_content : '<p>Add content here</p>',
'excerpt' => $post->post_excerpt,
);
// Return whatever allowed blocks was.
return $allowed_block_types;
}
<?php
add_filter( 'allowed_block_types', 'wps_enable_gutenberg_classic_block_for_some_post_types', 10, 2 );
/**
* Adds a default Classic block if no default content exists for a specific set of post types.
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param \WP_Post $post The post resource data.
*
* @return bool|array
*/
function wps_enable_gutenberg_classic_block_for_some_post_types( $allowed_block_types, $post ) {
// If not our post type, bail.
// Change [ 'post', 'page', ] to be whatever post types you want.
if ( ! in_array( $post->post_type, [ 'post', 'page', ], true ) ) {
return $allowed_block_types;
}
// Add a classic block.
global $initial_edits;
$initial_edits = array(
'title' => $post->post_title,
'content' => $post->post_content ? $post->post_content : '<p>Add content here</p>',
'excerpt' => $post->post_excerpt,
);
// Return whatever allowed blocks was.
return $allowed_block_types;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment