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: http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/ * Description: Creates a fake page without a 404 error (based on Sean Hickey’s Fake Plugin Page) * Author: Scott Sherrill-Mix * Author URI: http://scott.sherrillmix.com/blog/ * Version: 1.1 */ class FakePage { /** * The slug for the fake post. This is the URL for your plugin, like: * http://site.com/about-me or http://site.com/?page_id=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’; /** * 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 athttp://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.
Jeriko | 05-Jun-07 at 1:16 am | Permalink
Tried this one with both WordPress 2.1.3 and WordPress 2.2, but it seems that with permalinks activated, the fake post isn’t recognized
ScottS-M | 05-Jun-07 at 8:51 am | Permalink
Hmm. It seems to be working here (for example). I would have thought it would create a page at http://www.i-jeriko.de/about-me/ but you didn’t see one there? Oh after messing with my permalink structure it looks like it’s the date-based categorization. I’ll look into that and have something up later today.
ScottS-M | 05-Jun-07 at 10:23 am | Permalink
It ended up being that the rewrite rules weren’t matching anything in
WP->parse_request
before thethe_posts
filter when using a date permalink structure (I had thought WordPress would have a general page rewrite rule but it looks like it adds a new rewrite rule for each page created so/about-me/
doesn’t match/2007/05/02/whatever/
or/specific-page-name/
and gives a 404). We could elegantly solve this by adding rewrite rules or we keep it simple and catch it inthe_post
filter. I went with simple for this tutorial and just reset the 404 related$wp_query
variables.Thanks for catching that Jeriko. Let me know if the new version doesn’t work for you.
Jeriko | 06-Jun-07 at 8:17 am | Permalink
Works like a charm, thank you very much!
Vardis | 09-Nov-08 at 5:53 pm | Permalink
Works well here. Thanks.
Andy | 26-Jan-10 at 7:24 am | Permalink
Hi Scott
This plugin looks exactly like what I need and works almost perfectly for my needs but have you got any ideas to see the content on the latest 2.91 version of WordPress?
On 2.91 I don’t get the following output anywhere.
return ‘Hi there! You are viewing my fake post!’;
Andy | 26-Jan-10 at 8:01 am | Permalink
My bad – a plugin conflict caused this on 2.91!
Whilst I am here how would you want to be credited if I use the code above within a plugin we are writing currently?
Is something like a link to this post alongside something saying ‘Fake page’ fucntinality based on Fake Page Plugin 2 by Scott Sherrill-Mix ok?
ScottS-M | 26-Jan-10 at 1:37 pm | Permalink
Glad to hear it still works and you found it useful. Sure that sounds fine for credit. I’ll be curious to see what you’re making. Good luck.
Andy | 26-Jan-10 at 4:38 pm | Permalink
We have integrated your plugin within our plugin
and added the following in our plugin code:
/*
* The following ‘OurPluginClass’ is based on the Fake Page Plugin 2 by Scott Sherrill-Mix
* Plugin URI: http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
*/
We are now just trying to tidy it up a little and to get it to allow us to pass any content to it.
Once we have the finshed plugin we will let you know so you can have a play with it :)
Denzel Chia | 25-Mar-10 at 12:55 am | Permalink
Hi,
I used this fake post code for a plugin.
But it is not working in WordPress 3.0 alpha version.
I was wondering whether you can make it work in WordPress 3.0
Need to get it done ASAP.
I am willing to pay for this work.
Please contact me via my email.
Thanks.
Cedric N. | 19-Apr-10 at 9:44 am | Permalink
Hi,
Cool article. Is there an easy way to assign a custom template to the fake post?
Udegbunam Chukwudi | 11-May-10 at 3:10 pm | Permalink
Is there a way to get it to work with Scott’s Permalink Redirect WordPress Plugin http://scott.yang.id.au/code/permalink-redirect/
There seems to be a conflict. Thanks in advance
Dan | 28-May-10 at 5:22 pm | Permalink
@Cedric: I realize you probably needed this a month ago, but in case anyone else is looking for a way to use a custom template with this, I got it to work by adding a function getTemplate() to this class, and hooking it into template_redirect right before createPost returns, a la
add_action(‘template_redirect’, array(&$this,’getTemplate’));
I modified a lot of this code to be able to pass in slugs, titles, content, etc, and I supply the name of a template file to the constructor as well. getTemplate then accesses it as an instance variable:
function getTemplate(){
if(file_exists(TEMPLATEPATH . ‘/’ . $this->template)) include(TEMPLATEPATH . ‘/’ . $this->template);
else include(TEMPLATEPATH . ‘/page.php’);
exit;
}
Nique | 18-Jun-10 at 3:57 pm | Permalink
@Dan,
When i use your method, my ‘home’ page is poluted with the generated fake page.
You have to use it like this:
_template))
load_template(TEMPLATEPATH . ‘/’ . $this->_template);
die();
}
?>
Nique | 18-Jun-10 at 5:28 pm | Permalink
of course i mean this:
function getTemplate()
{
if(file_exists(TEMPLATEPATH . ‘/’ . $this->_template))
include(TEMPLATEPATH . ‘/’ . $this->_template);
else
include(TEMPLATEPATH . ‘/page.php’);
die();
}
Global Outsourcing | 04-Aug-10 at 9:32 am | Permalink
the plugin work with Wp 3.0.1 ? or which version avaible to use the plugin ?
Jason Phimosis | 28-Sep-10 at 6:43 pm | Permalink
I am just curious, does this have any relation to autoblogging such as the auto blog system? Also, is it considered ethical to do it?
Josh Salverda | 02-Jan-11 at 3:22 am | Permalink
Thanks Scott – Works like a charm in WordPress 3.0
I’m working on building a members directory plugin and rather than building hundreds of static pages for each member this is waaaay easier, lol.
Thanks again :)
Josh
Willian | 04-Jan-11 at 9:36 pm | Permalink
There’s a way to generate meta keywords on header?
E.g. define a variable “keywords = cars, motorsport” and header load it as meta keywords.
Andy Bailey | 26-Jan-11 at 4:54 pm | Permalink
bravo! this is just what I was after for my future plugin. thanks, no more requiring the plugin user to add a page with a shortcode in it to be able to output a public page with dynamic content. super!
Cyril | 31-Jan-11 at 7:09 am | Permalink
@Dan @Nique
Hello, I’m trying to get a custom template to display, but I can’t figure out where to put :
add_action(‘template_redirect’, array(&$this,’getTemplate’));
I added this code between the FakePage() and createPost() functions:
function getTemplate()
{
if(file_exists(TEMPLATEPATH . ‘/’ . $this->glock))
include(TEMPLATEPATH . ‘/’ . $this->glock);
else
include(TEMPLATEPATH . ‘/page.php’);
die();
}
But no luck so far. My custom template is called page-glock.php
Please help me ;-)
boris | 17-Mar-11 at 3:47 pm | Permalink
i want that the content of my fake page displays the posts of a custom_post_type ^^
is-it possible ?
Deadpan110 | 20-Jun-11 at 5:58 am | Permalink
This may have been written back in 2007 – but it has made my life very easy.
I am working on a specialised plugin that needs a lot of faked custom pages.
Simply extending the class for every page can save a lot of time:
class My_Account extends FakePage {
var $page_slug = ‘my-account’;
var $page_title = ‘My Account’;
function getContent() {
return ‘new content for My Account’;
}
}
function My_Account_init() {
new My_Account;
}
add_action(‘init’,’My_Account_init’);
Many thanks (and I hope the above is formatted correctly).
olylax21 | 02-Aug-11 at 4:23 pm | Permalink
@cyril
We need to do something similiar. We want to display a custom template to display. We also need the slug to be based on the URL of the current page. Has anyone done this?
in maya riviera | 02-Sep-11 at 9:03 am | Permalink
this is exactly what I wanted for my plugin. a plugin to add a page can generate a public page with dynamic content. pr thanks your contribution will be very useful indeed ..
Buy In Maya Riviera | 02-Sep-11 at 10:43 am | Permalink
By time I wanted a plugin for my site that is capable of generating a dynamic page content publishes. Your contribution is really very useful thank you very much.
Tahir | 08-Feb-12 at 8:02 am | Permalink
Any way to assign page template to the Fake page?
Hans | 17-Feb-12 at 9:51 am | Permalink
hey…Â i found a VERY easy way to use any custom page template.
before retuning the $post in line 124, add the following:
add_post_meta($post->ID, ‘_wp_page_template’, ‘your-template.php’, true);
Hans | 17-Feb-12 at 10:24 am | Permalink
is it possible to create fake pages for custom post types, for example for some wiki like thing where the front-end-user can create/edit content:
mywiki.org/articles/new post_type = ‘my_article’; used ‘new’ as slug, but it doesn’t work.
Hans | 07-Mar-12 at 10:43 am | Permalink
solved it myself:
add_rewrite_endpoint(‘edit’, EP_PERMALINK | EP_PAGES );
flush_rewrite_rules();
allows you to use /edit suffix on all permalinks & pages and simply $_GET them inside of the standard template used…
cheers
comment se soigner | 20-Dec-14 at 12:58 pm | Permalink
Bonjour, je souhaite de souscrire à ce blog pour obtenir la plupart des mises à jour récentes ,
donc où puis-je faire se il vous plaît aider .
online | 06-Oct-23 at 11:50 pm | Permalink
this plugin still works on wordpress 6.0. thanks.