plugin development - Render a different posttemplate from within the template_include action?

I am working on a webinar plugin that uses a custom post type to display the webinar pages. Those pages could be registe

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:

And is then redirected to:

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 but sees exactly the same content as if they had redirected?

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/my-post-type/mypost

And is then redirected to:

https://example/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/my-post-type/mypost but sees exactly the same content as if they had redirected?

Share Improve this question edited Nov 15, 2019 at 12:44 mikedavies-dev asked Nov 15, 2019 at 12:02 mikedavies-devmikedavies-dev 979 bronze badges 6
  • if you want to display the content on another URL, it's better to make URL rewriting rather than having same content on 2 URL : wordpress.stackexchange/questions/352189/… – Kaperto Commented Nov 15, 2019 at 12:07
  • The problem is that it depends on the settings of the custom post, i.e. some posts might have the setting enabled and others won't and then the custom post they link to might be different so I'm not sure that's a good fit? – mikedavies-dev Commented Nov 15, 2019 at 12:16
  • I am not sure to understand what you want to create. edit your question to give more details about what the CPT is and what users do with that object. – Kaperto Commented Nov 15, 2019 at 12:29
  • I've edited the question to give more detail, I hope that's clearer – mikedavies-dev Commented Nov 15, 2019 at 12:44
  • Would it be possible to post all of your code so we can see where you are using template_include? I think I have a grasp on what you need but would like to see your code first. – Tom Commented Nov 19, 2019 at 12:40
 |  Show 1 more comment

2 Answers 2

Reset to default 3 +50

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 );

The idea of template_include is to intercept the normal template procedure and replace the template with another one based on some conditions. What you are doing now is to redirect to another url which would, through the normal template procedure, produce the template you want.

You can get your desired outcome by simply let template_include do what it is meant for. Since I don't know exactly how you have stored stuff I can't give you precise code, but it would look like this:

add_filter ('template_include','wpse352621_custom_template',10);

function wpse352621_custom_template ($template) {
  if ('webinar' == get_post_type()) { // assuming this is the name of your cpt
    // now, with $custom_page_id, you must not retrieve the url, but the template.
    // you have asked the user which template he wants and stored it somewhere,
    // presumably as user data (*)
    wp_get_current_user();
    $user_template = $current_user->user_template // assuming this is where you stored it
    // now, just to be sure check if that template exists, then assign it
    $template_path = path.to.plugin.or.theme.directory . $user_template;
    if (file_exists ($template_path)) $template = $template_path;
    }
  return $template;
  }

(*) That would be the case if every user of the site can choose a template of his liking at this point. If you mean the admin can choose, you could store it as metadata to the post. Or you could even have a separate table somewhere in your plugin relating $custom_page_id's to templates. That does not fundamentally change what you need to do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744974562a4604066.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信