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

Am relatively new to Wordpress development and looking for the best way to customize the output for a specific javascript app that will be pulling in the content dynamically, by using a Wordpress plugin.

I can do most of what I need to do by overriding the feed template, allowing custom output by search term, tag, and category. However, getting an individual post for the feed (ie. using p= or page_id= query variables) doesn't seem to be an option by default.

What would be the best, cleanest way (ie. least amount of conflicts) to create the custom output post/page output? Simply add a new query_var filter and hook into parse_request, add new query vars for the feed somehow, or some other method to override the template for the content from within the plugin code?


share|improve this question

Do I understand right that you need to pull information from WP in JavaScript code?

In that case it would be appropriate to use WP's Ajax handler. See AJAX in Plugins in Codex.

If native feeds are very close to what you need another option is to register custom feed with add_feed() and use it with customized template or one of native feed template with tweaks.

share|improve this answer
Thanks for the note on the internal WP ajax handler, will help for the plugin settings interface - for this, the data is being drawn from an external site outside Wordpress. I'm currently using add_action('do_feed_myfeed', ...) to override for ?feed=myfeed, but seems I cannot pass in a specific page or post as a query param to limit the results just tag=, s=, and cat=. I could just add a custom request handler to handle something like /?p=3&tmpl=mytmpl if there's no native way to override the template in the plugin and have the content already loaded (rather than querying the db myself)? – Brian Hogg Sep 7 '11 at 22:01
@Brian Hogg sorry, I do not completely follow what you are trying to do (late here :) maybe you could add some simplified examples to question? Basically native feeds are not too open to the idea of query arguments. It partially works because of general WP query architecture, but it isn't really supposed to be used in that way. – Rarst Sep 7 '11 at 22:11
No problem :) I'm good on the feed side, but what I'm just trying to generate a unique url for each item in the feed that, when viewed from an external source, returns the content formatted in the way I specify (in my case, the html wrapped in a json object with some other bits). For example the default link for a piece of content might be mysite.com/?p=3, but would return the content in whatever the current theme happens to be. Is there a 'good' way to override the theme from my plugin by passing in another get param (ie. mysite.com/?p=3&template=mytemplate)? – Brian Hogg Sep 7 '11 at 22:15

I have had no problem using all standard wp query parameters with feeds, and thus no problem with the p= single post option.

See http://test.icalevents.com/ical-feeds/ scroll down - there is an example of an individual post feed (in this case and event custom post type, but same principle applies) called "christmas in july" which extracts a single post for that feed.

Maybe you need to relook at your code and how you are doing it. In this example the plugin is allowing normal wp_query to operate plus some filters to the query.

share|improve this answer
Strange, what version of Wordpress is that site running? On my local I upgraded the same codebase / db to 3.2.1 and it cuts out most other params, whereas an old 2.8 install leaves them alone. I believe I saw a post noting that this behaviour changed in Wordpress 3.x but can't find the reference off-hand. – Brian Hogg Sep 8 '11 at 2:02
Actually just noticed the meta generator on that site is WordPress 3.2.1, which means there is likely another difference between the two installations. Regardless I'll need to detect when a single post or page is queried in the feed (rather than a category/tag feed with just one result) and show a different formatting, so might need to use a parse_request or other hook to do it, and if it's not working on my fresh install feed query vars might not be the way to go... – Brian Hogg Sep 8 '11 at 2:11
up vote 0 down vote accepted

Looks like this does the trick, using the template_redirect hook within the plugin:

function my_app_request() {
    global $wp_query;

    if ( array_key_exists('mycustomvar', $wp_query->query_vars ) )
        include( dirname( __FILE__ ) . '/templates/content-single.php' );

add_action('template_redirect', 'my_app_request');

function my_query_vars($vars) {
    $vars[] = 'mycustomvar';
    return $vars;

add_filter('query_vars', 'my_query_vars');

And from within the content-single.php I can format how needed:

echo json_encode(array('title'=>get_the_title()));
// ...

Now any valid url can have mycustomvar=1 (or similar) tacked onto the url and voila, content loaded without having to manually parse p or page_id query vars. Seems cleaner than using a parse_request hook.

If there's a better/cleaner way to do the above please let me know!

share|improve this answer

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.