Send your own HTTP status code in WordPress

Hey guys, the last few months I’ve been working on a rather complex WordPress-website with data flowing both from WordPress and the customers external database. The external database holds all of their products and are administrated from a completely different system. All I wanted to do was to fetch that data and display it in their WordPress page without losing the WordPress context.

In order to accomplish this I made a php file, products.php, and a WordPress-rewrite saying that if someone tries to access /products load products.php. Now this was seemingly working just fine, the page loaded, I had access to the WordPress context and I could fetch my data as I pleased from the customers product database.

However when I ran my site through a broken link checker app I found that all custom rewrites to files “outside” of WordPress threw 404-errors. I’m still not entirely sure why this happens but I guess the file doesn’t match any criteria in the handle_404 function in the WP-class when it loads.

When this problem was uncovered I was short on time and after half a day of googling I found the WordPress function status_header() where I can override whatever status WordPress thinks this page should send. So as a quick and dirty fix I just ran status_header( 200 ); just before I render my page resulting in a satisfactory status header.

In my opinion this solution is okay but of course it’s not optimal. There should be some way to make WordPress recognise files that doesn’t serve WordPress content. If I get time I will look deeper into the issue.

If you have similar experiences, don’t hesitate to comment and share your thoughts.