Create Fake Pages in WordPress

Create Fake Pages in WordPress

People’s need is changing everyday, and so is WordPress. From a simple blogging software, WordPress has evolved into a somewhat feature-rich Content Management System. It is, therefore, not a surprise if we, users and developers, always try to do crazy things with WordPress these days, and creating virtual (fake) WordPress pages is simply one of those unusual objectives.

So, what do we need fake pages for anyway? What I find particularly useful about fake pages is that you can create a page with its own structure on the fly. For example, if you are a plugin developer, you might want some tabs for documentation (like what we have on WordPress.org’s plugin directory). Or, if you are a tutorial writer, you might want to show each step of the tutorial on a separate page with its own title, and want those pages to get indexed by search engines without issues.

In fact, someone has already come up with a solution, but it seems that such solution doesn’t work anymore. There are other solutions that you can easily find using Google, but in my opinion they are incomplete and, to some extent, hackish. If you want a plugin, you can check out Virtual Pages, a nice little one but is still in its beta stage. I don’t actually use that plugin because my need remains very simple, and doable without much coding (a statically structured and multi-page plugin documentation).

Preparing your contents

Within the scope of this article, I will go after the same thing, a plugin’s documentation with fake and statically structured child pages (i.e. static slugs and titles). We will create 5 fake pages, including Installation, Usage, Screenshot, Changelog, and Users’ Feedbacks. Of course it is possible to have dynamically structured fake pages (e.g. a tutorial with different titles and slugs from page to page), but let’s leave such issue for a follow-up, otherwise this article could get pretty long (and confusing).

Please note that I use the word ‘page’ in its original meaning (i.e. a web page), and not a post type in WordPress, so basically you can use any custom post type for this task. However, it is best to use WordPress page or a custom post type with hierarchical support, because, obviously, we are creating fake child pages here.

In order to have separate contents for each fake page, you can use some post meta, but for the sake of simplicity, I will just use the built-in multi-page feature of WordPress, i.e. the <!--nextpage--> tag (sounds unfamiliar?) FYI, in previous versions of WordPress you should see a <!--nextpage--> button next to the <!--more--> button, and its task is to split your post into multiple pages. The bad thing is, it has been removed from the core WordPress (don’t really know why though). Adding it back should be simple, but I will just remember and use the <!--nextpage--> tag instead.

Now, to actually use it, open your HTML editor and put that tag on a separate line between any contents you want to split, like so:

Looks cool, but is there any catch? Well, a rather obvious drawback is that you won’t have comments for those fake pages (which doesn’t matter in my opinion), and editing the contents of this real page can be a little slower than usual (because it has all the contents of those fake pages in one place).

Coding Time!

Let’s not waste any more time, shall we? Fire up your theme’s functions.php and put the below snippet wherever you want:

It allows you to specify the permalink structure you are going to use for your fake pages. Basically, we are asking WordPress to accept requests such as http://example.com/slug-to-a-real-page/fake-slug/ and use a page template file (in this case: page.php) to serve the contents.

These rewrite rules will ensure that WordPress only accepts certain fake slugs and nothing more. For example, a request to http://example.com/slug-to-page/unallowed-slug/ will be greeted with a 404 error instead of any contents from our actual page (there will be no 404 error if you have a real page at that location, of course).

After adding your custom permalinks this way, you will have to flush the rewrite rules so that your newly added rewrite rules are used. The fastest way to flush the rules is to visit Settings → Permalinks and then click on Save Changes. Now if you try to navigate to any fake URL, such as http://example.com/my-product-page/installation/, you will be greeted with the contents of my-product-page instead of any 404 error, sweet!

The next thing we have to do is to make our fake permalinks canonical. To do that, you can use something like this in functions.php:

Please note the use of trailingslashit and user_trailingslashit, they will help you get the correct permalinks you need. You can read more information about them here if interested.

Next, it is important to mention that every single page on your site will now have those fake child pages. To limit the availability of fake child pages to certain pages only, you can, again, use a post meta, or better, use a page template.

With a page template you can have a separate template file for your fake pages, and you won’t have to check for anything (just choose your desired page template in the Page Attributes panel and WordPress will automatically load it). In this example I will name my page template bwp-plugin-page.php.

To limit the use of fake child pages to only the page template you choose, put this in your functions.php:

All requests should now be handled correctly (i.e. canonical permalinks and correct template file loaded).

Next, you will have to match each page you’ve separated using the <!--nextpage--> tag to a specific fake page. In my opinion, it’s simplest to match using their ordinals. So in this case, the first page will be an overview of your plugin, the second will be Installation, and so on.

Notice the $page = $count; thing? It will effectively match split pages to your fake pages, so that a request to http://example.com/my-product-page/feedbacks/ will be served with the 5th page’s contents, etc.

Finally, you should also provide links to all fake pages so your visitors will be able to navigate through them easily:

That’s it! Refresh your fake pages and see them shining just like a perfectly real WordPress page!

Customization

The <title> HTML tag

One thing you will probably notice is that your fake pages’ title tags might have something like ‘Page <number here>’ as their suffixes. Changing this depends greatly on how you generate the title tag for your HTML document, but generally, you can use the optional $showing_title global variables1 to construct <title>, like so:

Custom Post Types

You can of course use other custom post types to achieve the same thing, but as far as I know, you can’t use a page template with a custom post type. It would not be a problem, though, because we already have a separate template for custom post type. So if you have a post type named ‘plugin’, just put in your theme’s directory a file named single-plugin.php and it will be used just like bwp-plugin-page.php.

To use a custom post type named ‘plugin’, you will have to change the rewrite rules in our example to something like this:

Next, remove the bwp_non_fake_template_redirect filter (we no longer use page template) and make sure you enable appropriate features for your post type, namely publicly_queryable and query_var2, so that our requests are happily accepted by WordPress.

So that’s it, I hope you enjoy this article! Any questions, suggestions are welcomed! Don’t forget to check out the follow-up that will address the dynamic title and slug issue. Till next time!

[...] After creating the custom post type “ticket” for data input, I want to have a stage (step-by-step) purchasing process, WordPress enjoys making this a little hard but thankfully I came across a page detailing how to Create Fake Pages in WordPress. [...]