Skip to content

Instantly share code, notes, and snippets.

Created October 5, 2012 10:34
Show Gist options
  • Save cubehouse/3839159 to your computer and use it in GitHub Desktop.
Save cubehouse/3839159 to your computer and use it in GitHub Desktop.
WordPress Fake Page Generator - Use in Theme/Plugin to create non-existant pages dynamically
// create fake page called "chat-room"
// modify function and variable names with "ABCD" to whatever you like
// modify variable $fakepage_ABCD_url to the fake URL you require
function fakepage_ABCD_detect($posts){
global $wp;
global $wp_query;
global $fakepage_ABCD_detect; // used to stop double loading
$fakepage_ABCD_url = "chat-room"; // URL of the fake page
if ( !$fakepage_ABCD_detect && (strtolower($wp->request) == $fakepage_ABCD_url || $wp->query_vars['page_id'] == $fakepage_ABCD_url) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
$fakepage_ABCD_detect = true;
// create a fake virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $fakepage_ABCD_url;
$post->guid = get_bloginfo('wpurl') . '/' . $fakepage_ABCD_url;
$post->post_title = "Page Title";
$post->post_content = fakepage_chat_render();
$post->ID = -999;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time('mysql');
$post->post_date_gmt = current_time('mysql', 1);
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
return $posts;
function fakepage_ABCD_render(){
return "My amazing pretend page :D";
Copy link

This code is extremely useful! Works perfect. Thanks!

Copy link

Thanks! Found a bug, though - line 23 should be fakepage_ABCD_render() to match the name of the function on line 49.

Copy link

zverush commented Sep 5, 2015

@daltonrooney just create

function fakepage_ABCD_render() {
    return 'fake';

Copy link

Not sure why yet but I'm getting Trying to get property of non-object in link-template.php on line 636 from this.

Copy link

itdahde commented Sep 21, 2016

Hi cubehouse,

thanks for the lightest and most functioning virtualizing code snippets, i tested more than 5 and your's the best and simplest way ;)

Nathansh, i had the same problem, in my case it belongs to the
$post->ID = -999;
I changed this to
$post->ID = -1;
and the error disappears.

Then i have another error, that belongs to the gantry template framework:

Notice: Undefined offset: 1 in /var/www/vhosts/ on line 482
Notice: Undefined offset: 1 in /var/www/vhosts/ on line 483

The code at this lines looking like below:
if ($template = $this->get_query_type($type[0])) return $template; elseif ($template = $this->get_query_type($type[1])) return $template; #482 elseif ($template = $this->get_query_type("$type[0]_$type[1]")) return $template; #483 else return $this->get_query_type('attachment');

By taking a look into the query $wp_query, if saw, that it orders post_type='attachment'
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'antenne' AND wp_posts.post_type = 'attachment' ORDER BY wp_posts.post_date DESC
but already $post->post_type='page' was set up. So i researched and found out that for the gantry template framework was necessary to set the post_type also for the query:
$wp_query->is_attachment = false;
and then i got the result without any error or notices

Copy link

zenerry commented Aug 17, 2022

Great job, this works like a charm!
Is there any way to call a custom page template and use retrieved API info?


Copy link

PBC43 commented Feb 21, 2023

Amazing ! Many thanks for your job :)

Copy link

I am amazed this is still relevant 11 years later. Surprised WordPress has had such a robust framework it hasn't needed much change to keep this mostly working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment