• I am trying to figure out how I can add in a block template from a plugin for specific url needs. We have a CPT being registered in the plugin and also a custom taxonomy. We want to have a sub-page of the CPT based on the custom taxonomy. ex( cpt-slug/post-slug/taxon-name/taxon-term )

    The idea is that we want to have a page that is based on the CPT item and pass in a specific taxonomy. This is easy to handle with add_rewrite_url function, but what we are wanting to do is to render a specific block template through the plugin. So this would be like using the template_include hook, but instead of a PHP template, render out a block template. We are then wanting to allow for the theme to basically override the block template to customize as needed. The template from the plugin is to have a base template for rendering.

    I was trying to use the get_block_templates hook, which somewhat works, but when I add the same template in the theme’s templates folder, I end up with two templates showing up in the FSE screen and only the plugin template is rendering out and not being overridden by the theme template file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter rbaugh

    (@rbaugh)

    For additional reference, using the cpt-slug as my CPT slug, the default block templates would be $theme/templates/archive-cpt-slug.html and $theme/templates/single-cpt-slug.html. For the url call where I want to have the taxon appended to the end of a post item’s url, I would then want to load in a template like $theme/templates/single-cpt-slug-{taxon-term}.html or $theme/templates/single-cpt-slug-{taxon-name}.html. This would allow me to render out a block template that is based on the taxon name or term.

    • This reply was modified 1 year, 6 months ago by rbaugh.

    Did you ever figure this one out? I’ve been hunting for a workaround to do the same thing since it doesn’t appear to be supported in FSE yet.

    Thread Starter rbaugh

    (@rbaugh)

    You can try and use this gist to help you out. I am doing this call in my CPT class. The issue-section is a query_var that I use to connect to a taxonomy and what I use to load in the template.

    This doesn’t load in the template from the plugin into the FSE and it doesn’t allow for making modifications through FSE, but it does allow for overriding the template in the theme. SO you can at least have a template in the plugin and then override by the theme. While you can actually make the edits to the theme template, they do not render based on this code.

    I haven’t gone back in to see if I can locate the modified theme template through FSE to load it instead of the actual file. Just have to lookup the wp_template post type that matches the file name, but not sure just yet how to allow for adding other FSE templates to this.

    Thanks so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Block Template in Plugin’ is closed to new replies.