Create a custom page with a plugin on a block theme?
-
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
- The topic ‘Create a custom page with a plugin on a block theme?’ is closed to new replies.