0

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.

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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