Fake pages and roots - a better way?

We’ve got a pretty complex application that’s based on WordPress. A good number of web pages in the app are NOT real WordPress pages - we don’t want them to even exist in WP-Admin … as there’s no way we can let client staff remove the pages as they’re fundamental to the “app”.

So … as we’ve done with other websites, we’re doing “fake pages”. This is how we’d normally do it - http://jsbin.com/uBuQOpIk/1/edit?html,output (sorry, no idea how to add the PHP file here)

The problem with that is it takes over SO EARLY in the WordPress rendering, that nothing like base.php, etc, would ever happen - so all the goodness of roots would either need modularising further, or duplicating - both of which I don’t want to do.

So … the route I’ve gone down I feel is hacky but works (but bldy awful!) -

In custom.php I’ve added -

add_filter('roots_wrap_base', 'roots_wrap_base_cpts'); // Add our function to the roots_wrap_base filter

function roots_wrap_base_cpts($templates) {
    global $wp, $custom_pages, $wp_query;

    $custom_pages = array(
        "marketing-tools" => 'templates/template-marketing-tools.php',
        "part" => 'page-templates/template-part.php'
    );

    $requested_slug = strtolower($wp->request);

    if (array_key_exists($requested_slug, $custom_pages)) {
        global $fakepage;
        $fakepage='';
        unset($wp_query->query['error']);
        $wp_query->query_vars['error'] = '';
        $wp_query->is_page = TRUE;
        $wp_query->is_singular = TRUE;
        $wp_query->is_404 = FALSE;
        $fakepage="templates/template-".$requested_slug.".php";
    } else {
    }
    return $templates;
}

And then after reading the wrapper intro “Filtering the Wrapper: Custom Post Types”, in base.php I’ve added -

global $fakepage;
if ($fakepage) {
    include(TEMPLATEPATH . "/" . $fakepage);
} else {
    include roots_template_path();
}

I’m cringing even posting this … it seems so hacky, but it works. It solves my immediate need but gives me two problems -

  1. If there’s a valid 404 for some reason, I’m overriding it on my fake pages, so the fail wouldn’t be spotted.
  2. Because we’re not using the page_id query var any more as in the JSBin code I posted, I’m relying on the URL - which could cause issues potentially - is it unique enough, are there duplicates created in wp-admin that we don’t want to be picked up by fake pages, etc.

Any thoughts? :smile:

Is there a reason you didn’t just create a custom post type with all your protected pages and remove its menu from the dashboard?

Hey hey :smile:

Yes - never even thought about that as an approach :slight_smile: Sounds a good idea - that way it follows WP standards. I’m actually wondering what the bare minimum code required for a custom post type is - not had to do this before :slight_smile: Will give it a go now. Thank you!