Advertisement
Guest User

Untitled

a guest
May 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2. /**
  3. * Defines the block templates.
  4. *
  5. * @todo add references to the custom blocks
  6. *
  7. * @return array
  8. */
  9. function get_block_templates() {
  10. return [
  11. 'template_one' => [
  12. 'name' => 'Template One',
  13. 'blocks' => [
  14. [
  15. 'core/image',
  16. [
  17. 'align' => 'right',
  18. ],
  19. ],
  20. [
  21. 'core/paragraph',
  22. [
  23. 'placeholder' => 'Natoque iure necessitatibus rhoncus nisl aut incidunt pellentesque torquent nonummy voluptatibus posuere maecenas illo nunc fugiat, laboriosam quas facilisis ea, orci? . Dolorum aenean dolor, corporis laborum! ',
  24. ],
  25. ],
  26. [ 'core/image' ],
  27. ],
  28. ],
  29. 'template_two' => [
  30. 'name' => 'Template Two',
  31. 'blocks' => [
  32. [
  33. 'core/paragraph',
  34. [
  35. 'placeholder' => 'Natoque iure necessitatibus rhoncus nisl aut incidunt pellentesque torquent nonummy voluptatibus posuere maecenas illo nunc fugiat, laboriosam quas facilisis ea, orci? . Dolorum aenean dolor, corporis laborum! ',
  36. ],
  37. ],
  38. [ 'core/image' ],
  39. ],
  40. ],
  41. ];
  42. }
  43.  
  44.  
  45. /**
  46. * Add the new menu item.
  47. */
  48. add_action( 'admin_menu', 'insert_new_items_admin_menu' );
  49. function insert_new_items_admin_menu() {
  50. $templates = get_block_templates();
  51. foreach ( $templates as $slug => $details ) {
  52. /*
  53. * Translators: Call to action to create a new story from a template.
  54. */
  55. $name = sprintf( __( 'Add New From %s Template' ), $details['name'] );
  56. add_submenu_page(
  57. 'edit.php?post_type=post',
  58. $name,
  59. $name,
  60. 'edit_posts',
  61. 'post-new.php?post_type=post&template=' . $slug
  62. );
  63. }
  64. }
  65. /**
  66. * Insert the chosen template.
  67. */
  68. add_action( 'init', 'insert_template' );
  69. function insert_template() {
  70. $template = isset( $_GET['template'] ) ? wp_unslash( $_GET['template'] ) : false;
  71. $templates = get_block_templates();
  72. if ( $template && array_key_exists( $template, $templates ) ) {
  73. $story_object = get_post_type_object( 'post' );
  74. $story_object->template = $templates[ $template ]['blocks'];
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement