Hack: Create a Simple Non-WordPress Page

I came across this issue while trying to build a sitemap script inside of a WordPress plugin. In the past, I’ve just created a stand-alone PHP page that accesses WordPress and displays the sitemap. But I wanted to keep this code inside the plugin code. The problem is, you typically use plugin code to create contain insider a WordPress frame. But I just wanted plain text here, not the header and footer and all of that.

So I thought that maybe you could hijack things before even the header for the page came out. Then you could output some text and exit without a single character of WordPress was sent. The key is to use the init hook and then check add_query_arg to see if the URL matches what you want. Here is an example.

add_action('init', 'wpaction_show_something');
function wpaction_show_something() {
	if (add_query_arg() === '/something') {
		echo('Something here!');
		die();
	}
}

So this gives you just a plain text result, even though WordPress is involved. You can even do a WP_Query here to get some information and display it. Thus, a sitemap.

But I think you could use this for a while lot more. Maybe a robots.txt or other miscellaneous URL, generated inside a plugin. Or even some utility pages that are bare-bones.

Leave a Reply

Your email address will not be published. Required fields are marked *