WordPress: Fake Post Plugin

Fake Post Plugin simply create a fake page, that doesn't actually exists in the database, but is viewable, and indexable by search engines. Using the above below, when someone visits the URL http://site.com/about-me or http://site.com/?page_id=about-me, the plugin then takes over, and creates a new fake post, and adds it to the $wp_query vars. When the single.php template is loaded, […]

Fake Post Plugin simply create a fake page, that doesn't actually exists in the database, but is viewable, and indexable by search engines. Using the above below, when someone visits the URL http://site.com/about-me or http://site.com/?page_id=about-me, the plugin then takes over, and creates a new fake post, and adds it to the $wp_query vars. When the single.php template is loaded, that one single fake post will be displayed.

This example will just show a fake post with the title About Me, and the content of the post will be "Hi there! You are viewing my fake post!".

You can easily change the GetContent() method to whatever you want. Whatever is returned by the method will be the fake post's content.

<?php /** * plugin name: fake page plugin * description: creates a fake page * author: sean hickey * version: 1.0 */class fakepage { /** * the slug for the fake post. this is the url for your plugin, like: * http://site.com/about-me * @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'; /** * the template file that will be used when displaying your fake post. */ var $template_page ="" 'single.php'; /** * class constructor */ function fakepage() { /** * we'll wait till wordpress has processed the query vars, and then * start rockin' and rollin'. */ add_action('parse_query', array(&$this, 'init')); } /** * called by the 'parse_query' action */ function init() { global $wp_query; // <-- important query stuff in here /** * now this works with permalinks, or regular ol' ?page_id="" urls. * what we're checking for is the name of the post requested by * the visitor. * the first part: $wp_query->get('name') == $this->page_name is * for when permalinks are being used. * The second part: $wp_query->query_vars['page_id'] == $this->page_name * is for when regular URLs are being used. That would be a URL * like: http://mysite.com/?page_id=about-me * If we've found the post we're looking for, then add an action * for when WordPress goes to load the template file. */ if ($wp_query->get('name') == $this->page_slug || $wp_query->query_vars['page_id'] == $this->page_slug) { add_action('template_redirect', array(&$this, 'TemplateRedirect')); } } /** * Called by the 'template_redirect' action */ function TemplateRedirect() { global $wp_query; /** * Make sure the user selected template file actually exists. If * not we're kinda screwed. */ if (file_exists(TEMPLATEPATH . '/' . $this->template_page)) { /** * 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. The questions and answers. */ /** * Clear out any posts already stored in the $wp_query->posts array. */ $wp_query->posts = array(); $wp_query->post_count = 0; /** * 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); /** * Now add our fake post to the $wp_query->posts var. When "The Loop" * begins, WordPress will find one post: The one fake post we just * created. */ $wp_query->posts[] = $post; $wp_query->post_count = 1; /** * And load up the template file. */ load_template(TEMPLATEPATH . '/' . $this->template_page); /** * YOU MUST DIE AT THE END. BAD THINGS HAPPEN IF YOU DONT */ die(); } } function GetContent() { return '<p>Hi there! You are viewing my fake post!</p>'; } }/** * Create an instance of our class. */ new FakePage; ?>

headzoo

WordPress, Fake Post Plugin