Generate a custom/fake/virtual page on the fly – WordPress Plugin Development

by
on January 17, 2012
(2 minute read)

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.

Please donate to keep this server up

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

I’ve got invites for a free bank transfer!

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).

Looking for cool gift ideas? TrendLiker.com lists hundreds of nice items and lets you know when they go on sale on Amazon.

Did you enjoy this article?

Thank with a coffee ☕

Or help me out by clicking one of these buttons:

    
    

2 comments

  • marc says:

    awesome xavi, de pm!

  • aaron says:

    Hi xavi,

    This is working fine for me when debug mode is set to false. However, when set to true I’m getting an error stating……

    Undefined offset: 0 on line $postid = $postid[0];

    Any idea why this might be happening?

    Thanks
    A

Follow me for cool new products and interesting findings on graphic design, web development, marketing, startups, life and humor.


/*Twitter*/ !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs"); /*Facebook*/ (function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) {return;}js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=28624667607";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk')); /*Google+*/ window.___gcfg = {lang: 'en-GB'};(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;po.src = 'https://apis.google.com/js/plusone.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);})();
Airbnb 25€ discount