Disabling your active WordPress theme for a particular page
Sometimes I need to host a totally custom page on a WordPress site that doesn’t inherit from the active theme.
For example, you might be featuring a particular new initiative at a specific URL; putting up some custom landing pages for paid ads or a unique marketing push; or want to build a campaign-specific microsite that still lives under your primary domain.
In all of these cases you may find that you need to build a design that’s very different from the rest of the site — a different layout altogether, totally different stylesheets, a totally different nav menu, potentially even different legal language and footer links, and so on.
Google searches like “custom specific page layout WordPress” or “WordPress disable active theme one page” tend to turn up unsatisfying answers:
If you’re using a WordPress page builder, you can just build a page from scratch using the page builder framework. I’m usually not using a WordPress page builder. And if I’m building against a client site that has a preexisting page builder set up, I still prefer not to use it for totally isolated landing pages with very specific and unique design and layout. I like writing HTML!
You can use a plugin like Multiple Themes to switch from one theme to another on a per-page basis. I usually don’t want to install a new plugin with potentially wide-ranging effects on a preexisting site just for one page. And in these scenarios I don’t exactly need to switch to an entirely different theme package — it’s more that I need to turn off the theme altogether so that I can build something from scratch with full control over its stylesheets, javascript, meta tags, and so on.
In functions.php code you can register actions that dequeue javascript, deregister stylesheets, remove filters, and so on. And you can do this conditionally based on the currently active URL. But this requires knowing in advance what the full set of active styles, scripts, and filters are, in order to name and remove them, which is obviously both laborious up front and brittle over time. I’d prefer to start from a blank slate and selectively whitelist individual effects from the broader site. This approach also requires hardcoding page paths, or adding some other layer of indirection to identify which pages should have the broader theme effects removed.
The answer here is one of those things that’s obvious in retrospect.
Just create a custom page template file, and … don’t use any of the standard WordPress functions in it. Don’t call get_header() anywhere, don’t call get_sidebar() anywhere, and don’t call get_footer() anywhere.
<html>
<head>
...your custom head code...
</head>
<body>
...your custom header code...
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content/content-page' );
endwhile;
?>
...your custom footer code...
</body>
</html>
If you want, you can even skip The Loop. Now your page template can be a totally static HTML file that doesn’t allow anyone to edit the content!1
Now you have full control over the emitted HTML for any page that uses this template, and you can also whitelist specific plugins’ effects back in as needed.2
This can be more useful than it sounds!
For example, consider re-enabling effects from Yoast or other SEO plugins, and any plugins for lazy-loading images and asset minification. But you might also just be taking care of those by hand in your static HTML or with special-purpose custom fields.