WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a plugin named foo, and I want when user typed url /hello and it will display the string world

There is an existing post which uses the following code

if ($_SERVER["REQUEST_URI"] == '/hello') {
  echo "world";

But this is hacky, as it terminates immediately. For example, it ignores the shutdown hook.

Are there any better approach allow me to print the output and follow the action lifecycle gracefully?

share|improve this question
Is the aim to use a template, for instance, to display content ('world') - but without completely by-passing WordPress or aborting it early? The linked post is to do with ajax - if so have you look at the proper way of handling ajax actions through WordPress? – Stephen Harris Nov 19 '12 at 14:56
@StephenHarris, I want to display the output no matter what template is used by the users. – Howard Nov 19 '12 at 15:14
You want to display it where? 'where' has a lot to do with which hook you use. – s_ha_dum Nov 19 '12 at 15:23
@Howard - I understand that. Could you explain what exactly you're trying to achieve? As the best answer will depend on that. My initial suggestion was to force WP to use a template you provide, which contains nothing but the content. – Stephen Harris Nov 19 '12 at 16:03
@StephenHarris, for example, I need to generate a virtual sitemap.xml without an actual file being written in the local file system. – Howard Nov 20 '12 at 15:53
up vote 1 down vote accepted

So it seems you want to leave it as a late as possible before setting what WordPress should return (in certain circumstances - e.g. when sitemap.xml is being requested).

There are two methods - which are broadly identical - but using the WordPress API do varying degrees.

The same principal applies to almost any content format (JSON, ICAL etc) and not just XML

Method 1: Add a feed:

Benefits of this is that WordPress immediately 'recognizes' the request. Unfortunately, it leaves you with 'feed' in the url - which you may/may not be desirable. For ICAL calendars, for instance, I think this method works well;

function eventorganiser_public_export(){
    add_feed('wpse73174', 'wpse73174_content_callback');    

Then the output is determined by wpse73174_content_callback() and is available at www.example.com/feed/wpse73174/ (depending on permalinks).

Method 2: Manual approach

A bit more 'hands on' is to use to redirect_template action. It's up to you here to decide if the query is for the content you are about to serve:

function maybe_wpse73174_content_callback(){
   if( /* we are after this content */ ){

The problem here is now deciding whether the user is requesting your content. You'll need to add your own rewrite rules / permastructures and query variables to recognize the query. (For JSON content its quite nice to add an endpoint). You might find these articles written by me at WP.Tuts relevant.

The callback

The callback function wpse73174_content_callback() is require to set the headers (if appropriate), print the content and then exit();

function wpse73174_content_callback(){
  echo 'Hello World';
share|improve this answer
thanks for your answer. You 2nd approach seems is what I need, but as it require a hard exit, so shutdown hook cannot be called. Of course, this might be the wordpress design fault and is a know limitation. – Howard Nov 21 '12 at 8:38
Strictly speaking it isn't necessary. But in the 2nd WordPress will try to load a template. One way would be to hook late onto template_include and return false if you are producing the output. But that assumes WP hasn't interpreted the query feed. Source: github.com/WordPress/WordPress/blob/3.4.2/wp-includes/… – Stephen Harris Nov 21 '12 at 9:42
In the 2nd method, as far as I'm aware, you are only missing out on the shutdown hook. – Stephen Harris Nov 21 '12 at 9:43
That made sense, thanks, – Howard Nov 21 '12 at 16:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.