Coprobo
Coprobo

Reputation: 3

WordPress replace page with a plugin

I am trying to make a plugin that will display custom page when a parameter is present in the URL. For example, let's say that the URL of a post is localhost/wp/2018/03/22/postname/, it displays post normally as expected. What I want to achieve, is when I specify URL as localhost/wp/2018/03/22/postname/?param=1, I want to display page that I will generate with the plugin. I made a function that works when the parameter is present and it echoes some example content, however, it merges with what WordPress normally displays. So when I put the parameter into URL I get a regular page with post and my content somewhere in the middle. I want to display only the page that i generate from scratch, from <html> to </html> with my plugin. How can I do that?

Upvotes: 0

Views: 284

Answers (1)

Muhammad Asad
Muhammad Asad

Reputation: 3803

There is a filter in WordPress called template_include this filter is executed before any output is displayed on the WordPress generated page.

You can override the current template via this filter as soon as you find the parameter and value in the URL. E.g. below is a filter I was using in a project to override archive & single template for a certain CPT:

function em_templates($template) {
  if(get_query_var('post_type')  == 'xyz'  ) {

          return FMM__DIR__ . '/templates/archive-xyz.php';
   }

    return $template;
}


add_filter('template_include', 'em_templates', 1, 1);

You can adopt this logic for parameter and use this WordPress filter to take over the template process and display yours own.

Upvotes: 1

Related Questions