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.

PHP:
  1. /**
  2. * Plugin Name: Fake Page Plugin 2
  3. * Plugin URI: http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
  4. * Description: Creates a fake page without a 404 error (based on <a href="http://headzoo.com/tutorials/wordpress-creating-a-fake-post-with-a-plugin">Sean Hickey's Fake Plugin Page</a>)
  5. * Author: Scott Sherrill-Mix
  6. * Author URI: http://scott.sherrillmix.com/blog/
  7. * Version: 1.1
  8. */
  9.  
  10. class FakePage
  11. {
  12.     /**
  13.      * The slug for the fake post.  This is the URL for your plugin, like:
  14.      * http://site.com/about-me or http://site.com/?page_id=about-me
  15.      * @var string
  16.      */
  17.     var $page_slug = 'about-me';
  18.    
  19.     /**
  20.      * The title for your fake post.
  21.      * @var string
  22.      */
  23.     var $page_title = 'About Me';
  24.    
  25.     /**
  26.      * Allow pings?
  27.      * @var string
  28.      */
  29.     var $ping_status = 'open';
  30.        
  31.     /**
  32.      * Class constructor
  33.      */
  34.     function FakePage()
  35.     {
  36.         /**
  37.          * We'll wait til WordPress has looked for posts, and then
  38.          * check to see if the requested url matches our target.
  39.          */
  40.         add_filter('the_posts',array(&$this,'detectPost'));
  41.     }
  42.  
  43.    
  44.     /**
  45.      * Called by the 'detectPost' action
  46.      */
  47.     function createPost()
  48.     {
  49.    
  50.         /**
  51.          * What we are going to do here, is create a fake post.  A post
  52.          * that doesn't actually exist. We're gonna fill it up with
  53.          * whatever values you want.  The content of the post will be
  54.          * the output from your plugin.
  55.          */  
  56.        
  57.         /**
  58.          * Create a fake post.
  59.          */
  60.         $post = new stdClass;
  61.        
  62.         /**
  63.          * The author ID for the post.  Usually 1 is the sys admin.  Your
  64.          * plugin can find out the real author ID without any trouble.
  65.          */
  66.         $post->post_author = 1;
  67.        
  68.         /**
  69.          * The safe name for the post.  This is the post slug.
  70.          */
  71.         $post->post_name = $this->page_slug;
  72.        
  73.         /**
  74.          * Not sure if this is even important.  But gonna fill it up anyway.
  75.          */
  76.         $post->guid = get_bloginfo('wpurl') . '/' . $this->page_slug;
  77.        
  78.        
  79.         /**
  80.          * The title of the page.
  81.          */
  82.         $post->post_title = $this->page_title;
  83.        
  84.         /**
  85.          * This is the content of the post.  This is where the output of
  86.          * your plugin should go.  Just store the output from all your
  87.          * plugin function calls, and put the output into this var.
  88.          */
  89.         $post->post_content = $this->getContent();
  90.        
  91.         /**
  92.          * Fake post ID to prevent WP from trying to show comments for
  93.          * a post that doesn't really exist.
  94.          */
  95.         $post->ID = -1;
  96.        
  97.         /**
  98.          * Static means a page, not a post.
  99.          */
  100.         $post->post_status = 'static';
  101.        
  102.         /**
  103.          * Turning off comments for the post.
  104.          */
  105.         $post->comment_status = 'closed';
  106.        
  107.         /**
  108.          * Let people ping the post?  Probably doesn't matter since
  109.          * comments are turned off, so not sure if WP would even
  110.          * show the pings.
  111.          */
  112.         $post->ping_status = $this->ping_status;
  113.        
  114.         $post->comment_count = 0;
  115.        
  116.         /**
  117.          * You can pretty much fill these up with anything you want.  The
  118.          * current date is fine.  It's a fake post right?  Maybe the date
  119.          * the plugin was activated?
  120.          */
  121.         $post->post_date = current_time('mysql');
  122.         $post->post_date_gmt = current_time('mysql', 1);
  123.  
  124.         return($post);   
  125.     }
  126.    
  127.     function getContent()
  128.     {
  129.         return '<p>Hi there!  You are viewing my fake post!</p>';
  130.     }
  131.  
  132.     function detectPost($posts){
  133.         global $wp;
  134.         global $wp_query;
  135.         /**
  136.          * Check if the requested page matches our target
  137.          */
  138.         if (strtolower($wp->request) == strtolower($this->page_slug) || $wp->query_vars['page_id'] == $this->page_slug){
  139.             //Add the fake post
  140.             $posts=NULL;
  141.             $posts[]=$this->createPost();
  142.        
  143.             /**
  144.              * Trick wp_query into thinking this is a page (necessary for wp_title() at least)
  145.              * Not sure if it's cheating or not to modify global variables in a filter
  146.              * but it appears to work and the codex doesn't directly say not to.
  147.              */
  148.             $wp_query->is_page = true;
  149.             //Not sure if this one is necessary but might as well set it like a true page
  150.             $wp_query->is_singular = true;
  151.             $wp_query->is_home = false;
  152.             $wp_query->is_archive = false;
  153.             $wp_query->is_category = false;
  154.             //Longer permalink structures may not match the fake post slug and cause a 404 error so we catch the error here
  155.             unset($wp_query->query["error"]);
  156.             $wp_query->query_vars["error"]="";
  157.             $wp_query->is_404=false;
  158.            
  159.         }
  160.         return $posts;
  161.     }
  162. }
  163.  
  164. /**
  165. * Create an instance of our class.
  166. */
  167. new FakePage;

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 http://site.com/about-me/ or for the non-pretty link people http://site.com/?page_id=about-me. 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