Creating a (Better) Fake Post with a WordPress Plugin

I was looking to create a fake page in a WordPress plugin I'm working on in my (all too limited) spare time. It may seem a little silly to try to create a fake page with a plugin but this could be useful for any plugin that will display information to the readers of the blog (outside the admin panel), like statistics, contact pages or about pages. Luckily there is a handy tutorial for creating a fake posts. But although the plugin cleverly tricks WordPress into displaying a post created dynamically by the plugin itself, WordPress is clever enough to know something is wrong and sends a 404 error before sending the plugin-created content. Although many browsers will still display the page, this is a problem for any text-based browser or if you want the page to be indexed by search engines and (for me at least) just kind of grates to know my magnificent new plugin isn't actually performing correctly. Anyway, this happens because WP->handle_404() called by WP->main() in classes.php checks how many posts were found which, in this case, is 0 since there is no real post for the requested URL. To get around this problem, we need to make sure we trick WordPress before this function is called. Luckily we can catch WordPress immediately after it (unsuccessfully) looks for posts before any other function can figure out anything is wrong by using the the_posts filter.

So here is the new working code or just download it here. I'm not sure how many people will actually be interested in it but if you are feel free to leave a comment with any suggestions or questions.

  1. /**
  2. * Plugin Name: Fake Page Plugin 2
  3. * Plugin URI:
  4. * Description: Creates a fake page without a 404 error (based on <a href="">Sean Hickey's Fake Plugin Page</a>)
  5. * Author: Scott Sherrill-Mix
  6. * Author URI:
  7. * Version: 1.1
  8. *//**
  9.      * The slug for the fake post.  This is the URL for your plugin, like:
  10.      * or
  11.      * @var string
  12.      */'about-me';
  14.     /**
  15.      * The title for your fake post.
  16.      * @var string
  17.      */'About Me';
  19.     /**
  20.      * Allow pings?
  21.      * @var string
  22.      */'open';
  24.     /**
  25.      * Class constructor
  26.      *//**
  27.          * We'll wait til WordPress has looked for posts, and then
  28.          * check to see if the requested url matches our target.
  29.          */'the_posts''detectPost'));
  30.     }
  33.     /**
  34.      * Called by the 'detectPost' action
  35.      *//**
  36.          * What we are going to do here, is create a fake post.  A post
  37.          * that doesn't actually exist. We're gonna fill it up with
  38.          * whatever values you want.  The content of the post will be
  39.          * the output from your plugin.
  40.          */  
  42.         /**
  43.          * Create a fake post.
  44.          *//**
  45.          * The author ID for the post.  Usually 1 is the sys admin.  Your
  46.          * plugin can find out the real author ID without any trouble.
  47.          *//**
  48.          * The safe name for the post.  This is the post slug.
  49.          *//**
  50.          * Not sure if this is even important.  But gonna fill it up anyway.
  51.          */'wpurl') . '/'/**
  52.          * The title of the page.
  53.          *//**
  54.          * This is the content of the post.  This is where the output of
  55.          * your plugin should go.  Just store the output from all your
  56.          * plugin function calls, and put the output into this var.
  57.          *//**
  58.          * Fake post ID to prevent WP from trying to show comments for
  59.          * a post that doesn't really exist.
  60.          *//**
  61.          * Static means a page, not a post.
  62.          */'static';
  64.         /**
  65.          * Turning off comments for the post.
  66.          */'closed';
  68.         /**
  69.          * Let people ping the post?  Probably doesn't matter since
  70.          * comments are turned off, so not sure if WP would even
  71.          * show the pings.
  72.          *//**
  73.          * You can pretty much fill these up with anything you want.  The
  74.          * current date is fine.  It's a fake post right?  Maybe the date
  75.          * the plugin was activated?
  76.          */'mysql''mysql''<p>Hi there!  You are viewing my fake post!</p>'/**
  77.          * Check if the requested page matches our target
  78.          */'page_id'//Add the fake post
  79. /**
  80.              * Trick wp_query into thinking this is a page (necessary for wp_title() at least)
  81.              * Not sure if it's cheating or not to modify global variables in a filter
  82.              * but it appears to work and the codex doesn't directly say not to.
  83.              *///Not sure if this one is necessary but might as well set it like a true page
  84. //Longer permalink structures may not match the fake post slug and cause a 404 error so we catch the error here
  85. "error""error"]=""/**
  86. * Create an instance of our class.
  87. */

This is just an example to get you started creating a fake page with a plugin. After loading this plugin, you will be able to visit your fake page at or for the non-pretty link people Hope it helps someone out and good luck with your plugin.

Change Log:

  • v1.1 6-5-2007
    • Fixed 404 error on more complicated permalink structures
  • v1.0 5-20-2007