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

In a plugin, I am building a custom page with

add_action('template_redirect', function(){
add_filter('the_title', function(){ return "Virtual Page Title"; });
add_filter('the_content', function(){ ?>
    <!-- HTML CONTENT -->
<?php }); ?>

But some templates wrap the page.php content calls in a have_posts() loop which I need to override for the virtual page. I have tried query_posts('posts_per_page=1') but that requires that there is at least one published post, which may not always be the case.

share|improve this question

How about something like this instead:

add_action('template_redirect', function(){
    echo 'page stuff';
add_filter('the_title', function(){ return "Virtual Page Title"; });

What you are trying to do strikes me as very prone to error. Different themes can do all kinds of different things with page.php, not to mention that it may not be present at all.

What you are doing actually works when I test it on Twenty Thirteen. If you are have trouble with some theme or another, take that as evidence of the preceding point-- this strikes me as prone to error. A lot of things happen in themes that your plugin has no control over. If you are going to create a page you probably need to create the bulk of it yourself.

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.