Creating fake WordPress pages
Published August 2nd, 2018 under Misc
Sometimes you may need to create a fake page within WordPress, which doesn’t actually exist. You could make a real page, then add a shortcode into it for addition of dynamic content. But this could confuse the site owners, and it’s just generally a bit messy, particularly if the URL needs to change regularly.
Thankfully Jamie Holding came to the rescue with an incredibly useful little bit of code he posted six years ago. It’s quite old, so I’ve updated it to match modern WordPress coding conventions and dropped out a global variable in favour of a constant (to prevent it doing the same task twice when the filter reruns).
You can see Jamie’s original code here … https://gist.github.com/cubehouse/3839159
<?php /** * Generate a fake virtual page within WordPress. * It will look and behave like a regular WordPress page, but is not present in the database. */ class Generate_Fake_Page { /** * Set some constants for setting options. */ const SLUG = 'fake'; // URL slug of the fake page. /** * Fire the constructor up :D */ public function __construct() { add_filter( 'the_posts', array( $this, 'generate_page' ), -10 ); add_filter( 'pre_get_shortlink', array( $this, 'shortlink_filter' ), 10, 2 ); } /** * Create a fake page called "fake" * * $fake_slug can be modified to match whatever string is required * * @param object $posts Original posts object * @global object $wp The main WordPress object * @global object $wp The main WordPress query object * @return object $posts Modified posts object */ public function generate_page( $posts ) { global $wp, $wp_query; if ( ! defined( 'FAKE_PAGE' ) && ( strtolower( $wp->request ) == self::SLUG ) ) { define( 'FAKE_PAGE', true ); // Create a fake virtual page. $post = new stdClass; $post->post_author = 1; $post->post_name = self::SLUG; $post->guid = home_url() . '/' . self::SLUG; $post->post_title = 'Some page name goes here'; $post->post_content = 'Stick some arbitrary text content in here'; $post->ID = 'cantusenumbershereforunknownreason'; $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 ); $posts = NULL; $posts[] = $post; // 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; unset( $wp_query->query[ 'error' ] ); $wp_query->query_vars[ 'error' ] = ''; $wp_query->is_404 = false; } return $posts; } /** * Shortlink filter. * * @param bool $return * @param int $id The ID of the post. */ public function shortlink_filter( $return, $id ) { $request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' ); $request_uri = explode( '?', $request_uri )[0]; // If on a fake page, then return true. if ( '/' . self::SLUG . '/' === $request_uri || '/' . self::SLUG === $request_uri // For when trailing slashes aren't in permalink setup. ) { return true; } return $return; } } new Generate_Fake_Page();
Daniel says:
What do you mean faux in bookmarks mean in woodpress
November 26, 2019 at 2:10 am # //
Ryan Hellyer says:
I didn’t say “faux”, so I’m not sure what you are referring to there sorry.
January 28, 2020 at 9:31 am # //
Maria Diggs says:
I have tried your code. But it’s not working perfectly. That’s why I am worried about it.
October 21, 2020 at 11:19 am # //
Ryan Hellyer says:
I’m happy to help, but would need to know what the problem was first.
October 21, 2020 at 12:05 pm # //
Lee Anthony says:
Code works, nice post!
September 18, 2022 at 3:51 am # //
hupe13 says:
See https://wordpress.org/support/topic/fake-page-doesnt-work-with-wp-6-1/
November 3, 2022 at 12:48 pm # //
Ryan Hellyer says:
Ohhh, thanks for sharing that! I’ll update the post accordingly.
November 11, 2022 at 2:54 pm # //
yolite says:
Excellent template!! I loved it! It’s everything I was looking for, thank you very much!
March 22, 2023 at 8:35 am # //