Posts and pages live in the database, but I have had a need in the past to create a page on the fly without it living in the db.
I’ve been thinking about how to fake wordpress into showing a post that is totally bogus without having a post in the db.
It turned out to be very simple. I hooked the wp action and checked for a 404. If there is no 404 then I told wordpress that it wasn’t really a 404 and supplied it with a page to display. WordPress thinks it came from the db.
Here is the code:
function kpg_f_content() { global $wp_query; if($wp_query->is_404 ) { $id=-42; // need an id $post = new stdClass(); $post->ID= $id; $post->post_category= array('uncategorized'); //Add some categories. an array()??? $post->post_content='hey here we are a real post'; //The full text of the post. $post->post_excerpt= 'hey here we are a real post'; //For all your post excerpt needs. $post->post_status='publish'; //Set the status of the new post. $post->post_title= 'Fake Title'; //The title of your post. $post->post_type='post'; //Sometimes you might want to post a page. $wp_query->queried_object=$post; $wp_query->post=$post; $wp_query->found_posts = 1; $wp_query->post_count = 1; $wp_query->max_num_pages = 1; $wp_query->is_single = 1; $wp_query->is_404 = false; $wp_query->is_posts_page = 1; $wp_query->posts = array($post); $wp_query->page=false; $wp_query->is_post=true; $wp_query->page=false; } } add_action('wp', 'kpg_f_content');
Make this into a plugin or add it to the functions.php file.
I need to expand it to inspect the url for clues as to what to display, but this is the basic skeleton for creating a bogus page in WordPress.
I use the wp action and check for 404 because all the work is done. All I have to do is tweak the objects to make wordpress think it actually found a post.
I have lots of databases that I can convert to pages on the fly and all I would need is the links to the pages somewhere. I could even have the links as bogus pages and then one link from the sidebar to the top bogus menu and google will find them all.
You can add a lot more information in the post object like date, author, etc, but I am not concerned with making it perfect, yet.
This would be perfect for integrating store software or bbs software into a blog. Using the post structure gives you total control over the look of the pages while a little custom code can make as many fake pages as there is data.