Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Last active January 10, 2025 03:58

Revisions

  1. justintadlock revised this gist Jan 16, 2024. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -36,9 +36,6 @@
    } );

    // Filter the default template types, adding your own.
    //
    // Note: I did run into some trouble adding custom types at some point in the
    // past, but I could never figure out why. This seems to work on 6.5/trunk.
    add_filter( 'default_template_types', function( $templates ) {
    return array_merge( $templates, [
    'changelog' => [
  2. justintadlock revised this gist Jan 16, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    # Bootstrap the theme.
    add_action( 'after_setup_theme', __NAMESPACE__ . '\\theme', PHP_INT_MIN );
    <?php

    // This filter specifically checks whether a theme has a `changelog` template.
    // If not, it looks for a fallback template in the plugin.
  3. justintadlock revised this gist Jan 16, 2024. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,14 @@
    <?php
    # Bootstrap the theme.
    add_action( 'after_setup_theme', __NAMESPACE__ . '\\theme', PHP_INT_MIN );

    // This is specifically looking for a "changelog" template type
    // This filter specifically checks whether a theme has a `changelog` template.
    // If not, it looks for a fallback template in the plugin.
    add_filter( 'template_include', function( $template ) {
    // Probably want to check a query var here.
    if ( ! isset( $GLOBALS['changelog'] ) ) {

    // I'm just testing a query string here for demonstration/testing
    // purposes, but you'll want to add a check for your registered query
    // var with WordPress. For now, you can test with `?changelog=anything`.
    if ( ! isset( $_GET['changelog'] ) ) {
    return $template;
    }

  4. justintadlock created this gist Jan 15, 2024.
    45 changes: 45 additions & 0 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    // This is specifically looking for a "changelog" template type
    add_filter( 'template_include', function( $template ) {
    // Probably want to check a query var here.
    if ( ! isset( $GLOBALS['changelog'] ) ) {
    return $template;
    }

    // Custom hierarchy of templates.
    $templates = [
    'changelog.php',
    'index.php'
    ];

    // First, search for PHP templates, which block themes can also use.
    $template = locate_template( $templates );

    // Pass the result into the block template locator and let it figure
    // out whether block templates are supported and this template exists.
    $template = locate_block_template( $template, 'changelog', $templates );

    // If the template is found, use it. Otherwise, fall back to the plugin
    // template. It's worth noting that the wrapping block template markup
    // and block processing wouldn't be available to the plugin template.
    // The plugin would have to handle all of this on its end and probably
    // add a few actions/filters that WP would normally add.
    //
    // I'd recommend digging through `wp-includes/template-canvas.php` and
    // the `locate_block_template()` function for building this fallback.
    return $template ?: '/path/to/plugin/fallback-template.php';
    } );

    // Filter the default template types, adding your own.
    //
    // Note: I did run into some trouble adding custom types at some point in the
    // past, but I could never figure out why. This seems to work on 6.5/trunk.
    add_filter( 'default_template_types', function( $templates ) {
    return array_merge( $templates, [
    'changelog' => [
    'title' => 'Changelog',
    'description' => 'A custom changelog template.'
    ]
    ] );
    } );