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] /** * Plugin Name: Fake Page Plugin 2 * Plugin URI: * Description: Creates a fake page without a 404 error (based on Sean Hickey’s Fake Plugin Page) * Author: Scott Sherrill-Mix * Author URI: * Version: 1.1 */ class FakePage { /** * The slug for the fake post. This is the URL for your plugin, like: * or * @var string */ var $page_slug = ‘about-me’; /** * The title for your fake post. * @var string */ var $page_title = ‘About Me’; /** * Allow pings? * @var string */ var $ping_status = ‘open’; /** * Class constructor */ function FakePage() { /** * We’ll wait til WordPress has looked for posts, and then * check to see if the requested url matches our target. */ add_filter(‘the_posts’,array(&$this,’detectPost’)); } /** * Called by the ‘detectPost’ action */ function createPost() { /** * What we are going to do here, is create a fake post. A post * that doesn’t actually exist. We’re gonna fill it up with * whatever values you want. The content of the post will be * the output from your plugin. */ /** * Create a fake post. */ $post = new stdClass; /** * The author ID for the post. Usually 1 is the sys admin. Your * plugin can find out the real author ID without any trouble. */ $post->post_author = 1; /** * The safe name for the post. This is the post slug. */ $post->post_name = $this->page_slug; /** * Not sure if this is even important. But gonna fill it up anyway. */ $post->guid = get_bloginfo(‘wpurl’) . ‘/’ . $this->page_slug; /** * The title of the page. */ $post->post_title = $this->page_title; /** * This is the content of the post. This is where the output of * your plugin should go. Just store the output from all your * plugin function calls, and put the output into this var. */ $post->post_content = $this->getContent(); /** * Fake post ID to prevent WP from trying to show comments for * a post that doesn’t really exist. */ $post->ID = -1; /** * Static means a page, not a post. */ $post->post_status = ‘static’; /** * Turning off comments for the post. */ $post->comment_status = ‘closed’; /** * Let people ping the post? Probably doesn’t matter since * comments are turned off, so not sure if WP would even * show the pings. */ $post->ping_status = $this->ping_status; $post->comment_count = 0; /** * You can pretty much fill these up with anything you want. The * current date is fine. It’s a fake post right? Maybe the date * the plugin was activated? */ $post->post_date = current_time(‘mysql’); $post->post_date_gmt = current_time(‘mysql’, 1); return($post); } function getContent() { return ‘

Hi there! You are viewing my fake post!

‘; } function detectPost($posts){ global $wp; global $wp_query; /** * Check if the requested page matches our target */ if (strtolower($wp->request) == strtolower($this->page_slug) || $wp->query_vars[‘page_id’] == $this->page_slug){ //Add the fake post $posts=NULL; $posts[]=$this->createPost(); /** * Trick wp_query into thinking this is a page (necessary for wp_title() at least) * Not sure if it’s cheating or not to modify global variables in a filter * but it appears to work and the codex doesn’t directly say not to. */ $wp_query->is_page = true; //Not sure if this one is necessary but might as well set it like a true page $wp_query->is_singular = true; $wp_query->is_home = false; $wp_query->is_archive = false; $wp_query->is_category = false; //Longer permalink structures may not match the fake post slug and cause a 404 error so we catch the error here unset($wp_query->query[“error”]); $wp_query->query_vars[“error”]=””; $wp_query->is_404=false; } return $posts; } } /** * Create an instance of our class. */ new FakePage; [/php] 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