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.
This was done in 2009. I think one of the functions that I used is now deprecated in the newest version of PHP. I am still using the plugin that I made on one of my sites.
Keith
if By this way i will be able to add a post without adding it to my database?
If you want something similar but not in 404 (i.e. to inject a dummy post in existing categories), check out https://stackoverflow.com/a/45674474
Pagination is beyond the scope of this procedure. There are plugins that will help.
how I can add pagination on it? I’m trying to place it on the homepage and search result. I’m using API to serve the content :v
Hi Keith,
Thanks for your approach. I needed to do something similar and it helped me get the idea. In my case I needed to have a nice custom URL instead of creating a page when 404ing and I finally came with a solution here:
https://xaviesteve.com/2851/generate-a-custom-fakevirtual-page-on-the-fly-wordpress-plugin-development/
The solution is pretty simple, a GET parameter triggers a function that forces the home page to load a custom title, content and page template.
I’ve tested it in my own website but it should work in all WordPress installations.
Since there is no page, there is no permalink. This solution only works if a page can’t be found – so it fakes one. You have to add code to look at the url that failed and based on that show different data for different fake pages.
Keith
Man I loved it, to get here I was losing my mind trying to figure out some hard mod rewrite rules, but your solution is far better! Did exacly what I needed!
Just one doubt, how can I set the_permalink value through this function!
Thanks a lot Sir!!!
This is just code to create the post. It does not provide content.
Keith
I am a bit hazy on the usage. Will it create a page containing any content dynamically?
I used to have a portal builder script which displays a page on the fly for any keyword you put at the end of the url – the content used to be pulled based on the keyword from article databases, youtube, amazon (with affiliate links),clickbank etc. and you had the flexibility to define your template with tokens, so you can add other advertising as well.
Will this coe do something similar in wordpress? Sorry for my ignorance.
I’ve been thinking about something like that for a long time. I’ve managed to do a few changes to get data from an external database, enjoy
function kpg_f_content() {
global $wp_query;
if($wp_query->is_404 ) {
/* Get the URL and split it at every ‘/’
for https://www.exemple.com/a/b/c/
you’ll get $section[1] == a, $section[2] == b, $section[3] == c
*/
$section = explode(‘/’,str_replace(get_site_url(),”,’https://’.$_SERVER[“HTTP_HOST”].$_SERVER[“REQUEST_URI”]));
if ($section[1] == ‘a’) { // What ever expression you like you put it here
/* […] */
status_header(200);
// If you don’t change that every crawler will get an HTTP header 404.
//That doesn’t change a thing for the browser, but might change your SEO.
}
}
}
Hello Keith,
Thanks for answering 😉
I don’t understand the implementation. For full disclosure, i’m using G-Lock double opt-in plugin which uses this method:
https://headzoo.com/tutorials/wordpress-creating-a-fake-post-with-a-plugin
So creating a post is not an option… (I think).
And SexyBookmarks is displaying on these fake pages because I guess they use the “page” templates I hard coded SexyBookmarks into.
So could I define a different page template for fake pages? That might be a solution, no?
Best,
C.
You keep the custom fields in post_meta table. If you are faking a post then there is no id to create the post_meta info.
If you create a post, however, then you get the post_id.
Once you get the post_id back you need to update the post_meta table.
add_post_meta($post_id, $meta_key, $meta_value, $unique);
Keith
Hello,
I am trying to define a custom field value for fake pages (specifically to hide sexybookmarks on these pages).
In a regular page the field name is: Hide SexyBookmarks
And the value is: true
How get I get this into these pages?
Thanks,
C.
Thanks by reply.
Yes, I created a category called “cars” and put there, but still don’t working.
You have to create the categories before you do the post.
To put something in the meta you’d have to hook the head and write them.
I’ve tried calling teh categories but it didn’t work.
Is the code right?
$post->post_category= array('uncategorized');
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.
I don’t need credit, but if you can find a place somewhere out of the way on one of your websites for link, it would be appreciated. My websites that need link energy are https://www.blogseye.com https://www.cthreepo.com and https://www.jt30.com. A link will help me with the search engine rankings and is as good as gold to me.
Keith
I’m using this as part of a plugin, how would you like to be credited?
Ill be working on some stuff for this..
for now, Id suggest including the following as alot of themes uses these:
$post->post_author = 1; //Set author as admin (or “name” like “john doe” or extracting source url name like “wikipedia” 😉
$post->post_date = date_i18n(get_option(‘date_format’),date(‘Y-m-d’)); // Set date as today, using option setting
$post->comment_status = ‘closed’; // open or closed
$post->ping_status = ‘closed’; // open or closed
Maybe the upper case FALSE?
I would like to see if the 404 header has already been written to the output. I wasn’t getting a 404 when I wrote this, but that was in WP 2.6, I think.
Hi,
I’ve created a plugin to insert dynamic posts and pages based in this article. It works (can see the dynamic content) but for some reason it returns as a 404.
Do you know how to fix it?
This is my code:
…
function kpg_f_content() {
global $wp_query;
if($wp_query->is_404 ) {
$id=-42;
$url = “https://www.google.com”; //source of content
$raw = file_get_contents($url);
$post = new stdClass();
$post->ID= $id;
$post->post_category= array(‘uncategorized’); //Add some categories. an array()???
$post->post_content=$raw ; //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=’page’; //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->is_page=true;
$wp_query->is_post=false;
}
}
add_action(‘wp’, ‘kpg_f_content’);
…
What I’m doing wrong?
Thanks in advance,
Pedro
I really like this.
One thing to expand possibly – when using it to display a page – can you get it to use a page template?
I’m looking into it – and will post back if I find anything.
Awsome idear, its taking htaccess rewrites a step up inintelligence – I get a lot of “information wrapping” ideas :p