I ran into a very interesting problem today. WordPress is great as a CMS, but sometimes for special functionality on specific pages you just need to go outside of the shell, but the great thing is, you can still use the framework right? By including
<?php require('./wp-blog-header.php'); ?>
on the top of your page, you still have access to all the WordPress functions and code library. Which is great, because if your Navigation is built through a WP page hierarchy, you can still have access to it, and any changes in the backend will be reflected on your orphaned page.
All is right with the world.
Or so it seems. The underlying problem with this is when you include the all encompassing “wp-blog-header.php” it tries to put this page in the framework. And the way the framework operates when permalinks are in place is completely database driven. So if an actual file exists in a sub directory, but it isn’t a part of the WordPress hierarchy, WordPress sends out a 404 header. Now there’s a conflict, because the browser is getting a 404 header, but a file actually exists in the location. Firefox and some versions of IE interpret this fine, but some versions of IE don’t. And more importantly, neither does Googlebot… which means you’re page isn’t going to get indexed.
Fortunately, there’s a relatively easy way to get around this. Basically, don’t include wp-blog-header. I can already hear the virtual screams:
But but but! I want the tags! Isn’t that the whole point?! *anger* *confusion* *lamentations*
It’s definitely the point. And luckily, there’s a relatively easy way to accomplish this. Basically, we just want to include the specific functions of WordPress without making this specific page or pages part of the CMS. So don’t include wp-blog-header.php at the top. Use this instead:
<?php
require('./wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
?>
This will only pull the specific framework functions without putting the page as a part of the whole. In essence, WordPress won’t look for the corresponding page or post with “/your-sub-directory/”, but you’ll still be able to use all of the functions such as wp_head().
And you thought nonsensical 404’s couldn’t be fun… for shame.
[...] at the post by the nice folks at Adrogen (a Denver website design company) and quickly implement the solution they offer. They suggest using the following 5 lines of code to include WordPress instead of just including [...]
This SOOOOO fixed the issue I was stressing about ALLL weekend. Thank You!!!!!!!!!!
No problem! Glad it could help!
thanks a lot! saves my ass!
This worked for me. Thanks!
I’m surprised more people haven’t written about this problem. This is the only place I found that mentions it.
Pete, many thanks, this post helped me out! I’m thinking the same as Josh – amazed I haven’t seen this anywhere else. Was debugging why Googlebot was throwing off 404 errors – wp_blog_header was the reason. Regards, Jason
Hey Jason,
Just glad it could help out. Its a really weird bug that hopefully will be corrected with subsequent releases, but if not, at least there’s this
Brilliant! Thanks.
yes! spent about 30 minutes on this problem … you saved me another 3 hours.
thanks so much.
May you live long and prosper my friend!!
I spent almost three weeks (sadly, I am not exaggerating)on trying to resolve this issue. Did many re-installations, search far and wide, turned so many things upside down trying to get to the bottom of it. Very hard to repro because if the error or the page gets cached, can change behavior…
THANK YOU!!!
(…and I so wish I could have found your post earlier)
So for anyone else having this issue, fix it as Pete says but make sure you empty IE cache before you test it.
Happy, happy!
that just saved me a lot of trouble. Glad I found you on google for “404 when using wp_blog_header.php”
(thought I’d put that phrase on this page again so you’re up at number one for the next weary soul)
Great solution to a really perplexing problem. Google will now love my site again. Thanks!
(FIST BUMP)
Thanks buddy!
This solution has resolved my big problem
SO glad I found this page via Google (‘wp-blog-header.php 404′ 1st result). I swapped the code in and the plugin works again. Thanks!
awesome, this saved my life.
Thanks so much!! This has just fixed a problem that I’ve been working on for a couple of hours!
I include this code and I am given the following error message:
Fatal error: Call to a member function init() on a non-object
I added the following between the require and class calls to fix it:
$wp =& new WP();
I am not a wordpress expert so I can not swear by this solution, but it worked for me.
This post was a lifesaver! Thank you.
DUDE, you are FREAKING LEGEND!
saved me hours of work.
Thank you so much!
Why can’t you just do this?
require(‘./wp-blog-header.php’);
header(“HTTP/1.1 200 OK”);
Just re-write the header information before it goes out to the browser and you get access to all the WP functionality?
Am I missing somthing?
Cheers,
Jt.
great stuff man. keep sharing these kinds of things.
What I am looking to do is similar. I am looking to have a static page as well as the blog appear on the main page of my wordpress site. Right now going to the settings section will only allow me to do one or the other. any feedback would be appreciated.
you just saved me hours of research. My site is a bit dynamic I wasn’t 100% sure it was a WP issue.
ThankYou
Works great thanks!
I have a few old websites that have their own databases and create static HTML pages using their own templates. I’ve been trying to merge those template with Wordpress themes but it seems new problems keep popping up everywhere. Thanks for solving a big one.
I don’t know if this is an equally efficient fix in terms of resources loaded, but another astute WP hacker found that at the time they merged WPMU into WP, the preferred way to include all the WP headers in to load wp-load.php instead of wp-blog-header.php.[1]
The net result is the following code:
I’m not sure if the define for WP_USE_THEMES is still necessary but I imagine it would be. So then you have
[1] http://wordpress.org/support/topic/integrating-wp-in-external-php-pages?replies=22#post-1568731
Your site ate my code … here it is again:
/* Short and sweet */
define(‘WP_USE_THEMES’, false);
require(‘../../../wp-load.php’);
Am I the only one to who this DOES NOT work?
I tried several different approaches, including this and those in the comments, I am still being redirected to install.php featuring an 404!
Why?! What is the suppose of this anyway … stupid Wp
Okay, my problem factor: I try to include and load wordpress inside my own php class. this seems to be the troublemaker … really dirty fast workaround: require the wp-config.php outside the class and pass the $wp-variable as a parameter to the class/method.
I’m sure there is a nicer way, but can’t find one at the moment.
Thank you so much! You just saved me hours of headaches!
Merci
Worked perfectly! It’s funny. I can use the wp-blog-header as long as I am on the home page of my site, but if I use it on a sub page, then it 404s! Either way, this fixed it. Thank you!
Thank you so much bro!
Thanks alot…problem solved in a sec…
Thanks so much for this. Site appeared to be working fine but then strange things started happening with Googlebot and Adobe Browserlab, pages not being indexed, but yet seemed to be working okay!?
I managed to trace the problem using the brilliant Firefox plugin LiveHTTPheaders to be something to do with wp-blog-header.php, I use a different CMS but use Wordpress for the blog section. Luckily, decided to Google the problem and found this! You have saved me from a massive headache, so thanks so much!
You are my hero! WOW, I am so glad I found this post.