I am working on a webinar plugin that uses a custom post type to display the webinar pages. Those pages could be register, thank you, countdown, live etc. depending on the visitors registration status and the current webinar status.

The plugin uses the template_include action to render content based on the current post status and the visitors status (if they are registered or not).

The plugin lets users choose a custom page for one of those webinar pages, like a custom registration page or a custom thank you page. It shows the user a list of their WordPress pages and lets them choose one, then it saves the post_id in wp_post_meta.

In template_include I’m getting the $custom_page_id from wp_post_meta and if it is set, I redirect the visitor to that page in template_include using something like this:

$redirect_url = get_permalink($custom_page_id);
wp_redirect($redirect_url);

So the visitor accesses my custom post url:

https://example.com/my-post-type/mypost

And is then redirected to:

https://example.com/some-other-post

What I really want to do is render the entire contents of $custom_page_id without redirecting and ideally pass in some meta data like the original post ID.

Is there any way I can render the full contents of $custom_page_id (including theme header & footer) without having to redirect so the visitor stays on https://example.com/my-post-type/mypost but sees exactly the same content as if they had redirected?

2 Answers
2

This was a bit tricky to solve your problem.
the template_include filter executed after the main query processed the current request. If you can filter the current request (query_vars) and update it accordingly then WordPress will display any post/page you wanted to… Simply filter the query_vars with the request filter. Check the following snippet. But doing this might have a bad effect on SEO.

add_filter( 'request', function( $query_vars ) {
    if( is_admin() ) return $query_vars;
    $_post = null;
    // find the queried post
    if( isset( $query_vars['post_type'], $query_vars['your-post-type'] ) && $query_vars['post_type'] == 'your-post-type' ) {
        // $query_vars['your-post-type'] will contains the post slug
        $_post = get_page_by_path( $query_vars['your-post-type'], OBJECT, $query_vars['post_type'] );
    } else if( isset( $query_vars['p'] ) ) {
        $_post = get_post( $query_vars['p'] );
        if( $_post != 'your-post-type' ) $_post = null;
    }
    if( $_post ) { // post found
        // get the redirect to page id
        $custom_page_id = get_post_meta( $_post->ID, 'custom_page_id', true );
        $custom_page = get_post( $custom_page_id );
        if( $custom_page ) { // valid page/post found.
            // set the query vars to display the page/post
            $query_vars[$custom_page->post_type] = $custom_page->post_name;
            $query_vars['name'] = $custom_page->post_name;
            $query_vars['post_type'] = $custom_page->post_type;
        }
    }
    return $query_vars;
}, 10 );

Leave a Reply

Your email address will not be published. Required fields are marked *