0

I have created a custom plugin for WP that has an action that includes a template via template_include as shown here:

public function __construct( $wp_custom_plugin, $version ) {

    $this->wp_custom_plugin = $wp_custom_plugin;
    $this->version = $version;

    add_action('template_include', array( $this, 'add_my_template' ));    

}

Here is the function that references the template:

public function add_my_template($template){
  if ( is_page( 'my-unique-page' ) ) {
      $template = plugin_dir_path( __FILE__ ) . 'page-my-unique-page.php' ;
      return $template;
    }
  }
}

Okay, so my expectation here is that i would only see this template on my-site.com/my-unique-page. In practice I am not seeing this restriction, but rather the template is applied site wide. Any suggestions on how I can modify to limit the display to the single page? Thanks.

1
  • You may need to remove the return $template; line out of the 'if' statement, so cut and paste it just before the closing } of the add_my_template function. You should return the template even if you didn't change it.
    – WebElaine
    Commented Sep 18, 2017 at 14:52

1 Answer 1

0

Thanks to @WebElaine's comment. I needed to move the template return outside of the function so that it looks like this:

public function add_my_template($template){
  if ( is_page( 'my-unique-page' ) ) {
    $template = plugin_dir_path( __FILE__ ) . 'page-my-unique-page.php' ;
  }
  return $template;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.