› Solution for ‘Page not found’ title on page located outside of WordPress

Solution for ‘Page not found’ title on page located outside of WordPress

Email this to someoneShare on Facebook0Share on Google+0Share on LinkedIn0Share on Tumblr0Share on StumbleUpon0Tweet about this on TwitterShare on Reddit0Pin on Pinterest0

In some situations, one may need to use a page outside of WordPress for special features. In my specific situation, a client wanted to migrate a static site to WordPress while retaining the functions of a custom-coded calculator. I had trouble integrating the calculator into a custom page template, so I left the page outside of WordPress and called the blog header and theme to integrate with and match the theme’s styles:

<?php
/* Using WP with a static page outside of the WP installation */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

The problem: The page outside of WordPress worked correctly, but the title read “404 – Page Not Found.” This is because the page is not part of WordPress, so WP defaults to the 404 page title.

The solution I came up with for this problem is to create a custom header for the page outside of WordPress. It seems simpler than creating a custom title filter for one-time use.

So, copy the code from your header.php file to something like header-custom.php, and then change the code between the title tags to your custom text (this will display instead of the 404 – Not Found warning you may see by default). And then edit the call to the header in the page outside of WordPress as follows:

<?php get_header(); ?>

to:

<?php get_header('custom'); ?>

Now your custom page will display the custom title without interfering with the main site’s header.php file.

The above refers to the name of your custom header file. So if your custom header file’s name was header-blah.php, the code would be:

<?php get_header('blah'); ?>

Subscribe for weekly tutorials