Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In a plugin, I am building a custom page with

add_action('template_redirect', function(){
    include(TEMPLATEPATH."/page.php");
    exit();
});
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

1 Answer 1

How about something like this instead:

add_action('template_redirect', function(){
    get_header();
    echo 'page stuff';
    get_footer();
    exit();
});
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

 
discard

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.