• Resolved jlgallego99

    (@jlgallego99)


    Hello, I am currently developing a WordPress plugin and I need that plugin to create a custom page on an endpoint on the WordPress site with some of my content. I want to achieve something like, for example, The Events Calendar, that creates an ‘event/’ page that displays a calendar but maintains all the other parts of whatever website the plugin is installed (the foother, header, etc).

    I am trying to define a page with the rewrite API like this:

    add_action('init', 'register_endpoints');
    function register_endpoints(): void
    {
        add_rewrite_rule('^my-custom-page/?', 'index.php?custom_page=my-custom-page', 'top');
    }
    
    add_filter('query_vars', 'add_query_vars');
    function add_query_vars(array $vars): array
    {
        $vars[] = 'custom_page';
    
        return $vars;
    }
    
    add_filter('template_include', 'add_pack_template');
    function add_pack_template(string $template): string
    {
        // Check if it's the custom page
        if (get_query_var('custom_page') == 'my-custom-page') {
            // Set template path
            $template_path = __DIR__ . '/partials/my-custom-page.php';
    
            // Check if template file exists
            if (file_exists($template_path)) {
                return $template_path;
            }
        }
    
        return $template;
    }

    And my template looks like this:

    <?php /* Template Name: My Custom Page */ ?>
    <?php get_header(); ?>
        <div id="primary" class="content-area">
            <p>Hello world</p>
        </div>
    <?php get_footer(); ?>

    The problem is, I am currently using a block theme, so a warning shows that get_footer() and get_header() aren’t defined, and while my custom contents are displayed on the page, the rest of the page doesn’t maintain the theme and is raw HTML. How can I define my template to work with a block theme?

    And also, is this the best way to handle this? Could anyone provide me with tips or where to look at for the best possible way to do this?

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey @jlgallego99, did you ever figure this out?

    If not, this is what I would try (untested):

    if ( ! is_null( block_template_part( 'header' ) ) ) {
        block_template_part( 'header' );
    } else {
        get_header();
    }

    Documentation on block_template_part. As you see, it should return null if the file is not found, or it’s empty.

    Thread Starter jlgallego99

    (@jlgallego99)

    Hi @zex2911 thanks for your input. As block_template_part is a void function, I had to check with the wp_is_block_theme() function. With that it works on a block theme, but I’ve noticed that the template doesn’t load any css. When I change to a normal theme, the CSS is loaded, but on a block theme the CSS is not loaded in either header, footer or my custom content

    Thread Starter jlgallego99

    (@jlgallego99)

    Perhaps there’s a better way to do all this? All I want is for my plugin to create a custom endpoint and insert some HTML content there, regardless of what theme the site is using

    Using the template_include filter on a block theme loads my template first, and then loads the home page of my site at the bottom. I’m using twentytwentyfour theme btw

    • This reply was modified 9 months, 2 weeks ago by jlgallego99.
    Thread Starter jlgallego99

    (@jlgallego99)

    I finally solved it, the problem was in my template, my theme uses a template-canvas.php file located on wp-includes that creates all the skeleton for the template and then inserts the content. Now my template looks like this:

    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo('charset'); ?>"/>
        <?php wp_head(); ?>
    </head>
    
    <body <?php body_class(); ?>>
    <?php
    wp_body_open();
    
    if (wp_is_block_theme()) {
        block_template_part('header');
    } else {
        get_header();
    }
    
    echo 'My content';
    
    if (wp_is_block_theme()) {
        block_template_part('footer');
    } else {
        get_footer();
    }
    
    wp_footer();
    ?>
    </body>
    </html>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Create a custom page with a plugin on a block theme?’ is closed to new replies.