Recently a plugin author released a cool little plugin that creates a more dynamic “About Me” style page. When the plugin is activated, it will create a physical page in the database. The content of that page would have some PHP function calls for the author’s plugin. This plugin also required the Exec-PHP plugin to be installed, so that the PHP function calls in the page could be executed.
There is a better way though! A better way than actually creating a page with PHP code in it, and requiring the Exec-PHP plugin. The author’s plugin could simply create a fake page. A page that doesn’t actually exists in the database, but is viewable, and indexable by search engines.
Here is the code for making this happen.
05a627619342696d1f71437222ce763a000
Using the above code, when someone visits the URL http://site.com/about-me (or http://site.com/?page_id=about-me), your plugin
takes over. It then 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.
Enjoy!
To copy the code from above.
November 30th, 1999 at 12:00 am
Thanks for the great tutorial. I modified the code a bit to fix the 404 error. It ended up being more changes than I could easily leave in a comment so I posted them here. Hope that’s ok. Thanks again.
November 30th, 1999 at 12:00 am
Oops looks like no links from inside the comments. I put the address in this comment’s website field. Sorry about the comment clutter.
November 30th, 1999 at 12:00 am
I don’t understand - why would anyone actually want to create a fake page? What are the advantages to doing such a thing?
November 30th, 1999 at 12:00 am
If you would call that fake post “guestbook” or something likely, you could build a bot-trap (bots love guestbooks =D) just by using a plugin.
November 30th, 1999 at 12:00 am
@Jim - Like I said at the top of the post, some plugins require their own page, or post. The Ask Me plugin is one of many - http://www.wpplugins.org/plugin/ask-me
The plugin lets users ask the blogger questions, and when the blogger responds to those questions, the Q&A appears on the blogger’s “Ask Me” page.
An example - http://ilovecode.com/qanda/
When this plugin is activated, it physically creates a page in the user’s database. That page has a few Ask Me function calls inside of it, wrapped up in some tags that tell Exec-PHP to execute those function calls.
The purpose behind this post is to show how you can forget the page creation process altogether. Your plugin can intercept a specific permalink request (in this case site.com/about-me), and display whatever your plugin is supposed to display on the page. There’s no need to actually create a real page.
November 30th, 1999 at 12:00 am
I may not have made this perfectly clear, but the reason for taking this approach, is because you want WordPress to do the work of displaying your plugin’s output. With that, your plugin’s output will match the user’s selected theme.
You could simply catch the permalink, and spit out some HTML. But then that HTML wouldn’t match up with the user’s theme. So by taking this approach, your content integrates perfectly into the user’s blog.
November 30th, 1999 at 12:00 am
This is a really cool idea. One reason I’d want to use it is to be able to use pure PHP in my posts, as opposed to WordPress-processed PHP. For example, try using the “next” tag in the middle of a PHP function in a post. It doesn’t work. Another use is if you want to have pages that break WordPress rules. For example, WordPress doesn’t allow page slugs of “search” or “comments”. With this plugin, you could potentially do that.
But Sean, I’m having a problem. When I make a fake page, it only works when I call it with “?page_id=”. The slug doesn’t work (site.com/slug/). Any ideas? Rewriting is enabled on my site.
November 30th, 1999 at 12:00 am
@Dave,
The method hasn’t been fully explored yet. I can tell you that the current implementation requires the use of your site’s permalink structure.
That means if you site is using a permalink structure like this:
site.com/2007/01/01/name-of-post
Then you would access the fake post using a similar structure, like:
site.com/2007/01/01/about-me
I’m sure there is a work around, and I’ll look into it.
November 30th, 1999 at 12:00 am
Okay, found a work around, and the rewritten code from above is here:
http://bin.headzoo.com/bin/53
November 30th, 1999 at 12:00 am
Awesome. That works. Excellent work.
November 30th, 1999 at 12:00 am
Another nice aspect of this implementation, which Sean is cearly too modest to mention, is that you can use PHP’s OO inheritance capabilities to create multiple versions/pages by subclassing FakePage once for each page and simply changing the class variables and making a new GetContent function. This is a very clean implementation, nice work Sean.
November 30th, 1999 at 12:00 am
Does this also work for pseudo-permalinks? (like hxxp://domain.tld/index.php/year/month/pageslug?
November 30th, 1999 at 12:00 am
preved ot slesarya Vasi
November 30th, 1999 at 12:00 am
Hi,
I’ve tried this and found that if you test the page with a basic browser (specifically: try lynx) wordpress actually responds with a “404 page not found header” before delivering the page content.
The page “appears” to work find in firefox but the header is still there, you then run into issues when submitting the page to validator service (or similar) as it sees the “404″ and has an error.
Do you know how to fix this ?
Cheers,
Nick
November 30th, 2007 at 7:18 am
It is really a good idea, what I learn from this is that we CAN combain some other applications to it, eg.,gallery.
P.S. I had translated it into Chinese and posted it on my blog
Thanx!