
I’m currently developing a WordPress plugin that needs to show a virtual page generated by my plugin (such as a form or a message) and I wanted to avoid adding a new Page and modifying the current WordPress installation, so every plugin form, message or page had to be generated on the fly without touching the database.
Some WordPress plugins or themes create new pages in the website’s installation to add advanced functionalities. As an example, WooCommerce adds new pages to the wp_posts database that help set up the website to work with it. In my opinion this is a mess, if you already have a WordPress website running, installing this theme/plugin will mess with your current page structures and whenever you want to uninstall it or deactivate it you will need to manually remove those pages created previously by the plugin. I once wanted to test an advanced WordPress theme that deleted all the current pages I had… that was the most annoying thing ever, since I already had written the content for the website and lost it (yes, I should have backed up but, who would expect a theme to delete and install pages without even a warning?).
A few weeks ago I posted a question in WordPress StackExchange but couldn’t really get the answer I was looking for, there was this solution to create fake posts but I wanted generate unlimited pages with simple code as possible and using friendly URLs like: http://example.com/?plugin_page=myfakepage (I could then easily use mod_rewrite to make them look even better). Today I kept investigating and finally came with the solution.
Step by step guide
We will load the page at http://example.com/ but modify its title, content and select a blank theme template so it looks like a complete new page.
Custom page content
First of all we create a function that will overwrite the content:
function plugin_myown_content() {
$return = '
<p>Fill in this form:</p>
<form action="?" method="post">
<input type="text" name="foo" value="bar" />
<input type="submit" value="Connect" />
</form>
';
return $return;
}
Custom page title
Now we overwrite the title of the page with this function:
function plugin_myown_title() {
return "On the fly foobar form";
}
Default page template
With this function we force WordPress to load the default page template of the current active theme:
function plugin_myown_template() {
include(TEMPLATEPATH."/page.php");
exit;
}
Hook actions and filters
Finally, we add a simple ifto check if the user is requesting the plugin’s fake page and run WordPress hooks to load the 3 functions mentioned before and force WordPress into displaying the virtual page we have just generated.
if ($_GET['plugin_page'] == "myfakepage") {
add_filter('the_title','plugin_myown_title');
add_filter('the_content','plugin_myown_content');
add_action('template_redirect', 'plugin_myown_template');
}
The code is pretty basic, from here you can add your desired PHP code to make it do whatever you want. You can also add more hooks to customize the page even further. I hope you get the idea. This code has been tested with the latest version of WordPress (3.3.1).
Comments (8)
This will not work if the the theme you are using does not have a page.php
February 6th, 2012 at 14:44Edgar, you can rename “page.php” to “index.php”, “single.php” or whatever other file your theme has and it will work.
February 6th, 2012 at 14:50Although page.php is an essential file of any WP theme, why would you develop a theme without the page.php?
[...] Found a a complete great offer better and simpler operating solution: Generate a custom/fake/virtual WordPress site near to the fly [...]
February 6th, 2012 at 17:18Brutal Xavi! Hem vindrà de perles per un projectet que estic preparant!! Bona feina!
February 22nd, 2012 at 11:31Hi,
I saw your post and thought maybe you can help me.
I’m trying to put logo’s on virual generated pages. I’ve tried to doe it with <?php if(is_page()). But ofcource thats not working beacuse there is only one page id, on which the other pages are generated. My virtual pages are generated by a forum plugin, so I have to work around it. Do you have any idea how i could work around the "one idea problem?"
thanks,
Makar
May 29th, 2012 at 14:59Great start, but a couple major problems with this code:
June 1st, 2012 at 20:18– If your home is set to show latest posts, your ‘fake page’ will displayed over and over again.
– If you have any widgets that loop over posts, this replaces the titles of all of them with their own titles
Why not do this?
[code]
October 28th, 2012 at 11:57function MYFUNC_Check_Page_Hooks() {
if ($_SERVER['REQUEST_URI'] == "/MyFakePage") {
get_template_part('header');
echo "MY PAGE CONTENT HERE!!!";
get_template_part('footer');
die();
}
}
add_action('template_redirect', 'MYFUNC_Check_Page_Hooks');
[/code]
Hi David,
Could you please explain more details.Such as which part of code should put in which file???I am little bit cofused here.
Thanks
May 15th, 2013 at 04:41