Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active June 19, 2024 21:30
Show Gist options
  • Save RadGH/fd9b434c37698e8f2a75b557771e4d06 to your computer and use it in GitHub Desktop.
Save RadGH/fd9b434c37698e8f2a75b557771e4d06 to your computer and use it in GitHub Desktop.

Revisions

  1. RadGH revised this gist Jun 19, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions wp_replace_block_page_template.php
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,8 @@ function rs_replace_create_post_block_template( $template ) {
    $_wp_current_template_content = $post->post_content;
    }

    // Note that block themes do not replace $template. If we did that, the block template would not be parsed.

    }

    return $template;
  2. RadGH revised this gist Jun 19, 2024. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion wp_replace_block_page_template.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,10 @@
    * @return string
    */
    function rs_replace_create_post_block_template( $template ) {
    if ( get_query_var('bb_crm_action') === 'create_post' ) {

    // Replace this conditional with your own logic to target a specific page or url:
    if ( get_query_var('my_action') === 'create_post' ) {

    global $_wp_current_template_id, $_wp_current_template_content;

    // Use a block template from the theme
    @@ -23,6 +26,7 @@ function rs_replace_create_post_block_template( $template ) {
    $_wp_current_template_id = wp_get_theme()->stylesheet . '//' . $block_template_slug;
    $_wp_current_template_content = $post->post_content;
    }

    }

    return $template;
  3. RadGH revised this gist Jun 19, 2024. No changes.
  4. RadGH revised this gist Jun 19, 2024. No changes.
  5. RadGH created this gist Jun 19, 2024.
    31 changes: 31 additions & 0 deletions wp_replace_block_page_template.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php

    /**
    * Before the block template is displayed, replace it with a different block template.
    *
    * IMPORTANT: You must create the block template through the editor: Templates > Add New
    *
    * @param string $template
    *
    * @return string
    */
    function rs_replace_create_post_block_template( $template ) {
    if ( get_query_var('bb_crm_action') === 'create_post' ) {
    global $_wp_current_template_id, $_wp_current_template_content;

    // Use a block template from the theme
    $block_template_slug = 'wp-custom-template-crm-create-post';

    // Get the block template post in the database
    $post = get_page_by_path( $block_template_slug, OBJECT, 'wp_template' );

    if ( $post ) {
    $_wp_current_template_id = wp_get_theme()->stylesheet . '//' . $block_template_slug;
    $_wp_current_template_content = $post->post_content;
    }
    }

    return $template;
    }

    add_filter( 'template_include', 'rs_replace_create_post_block_template' );