1

I´am creating a Plugin. For this I wrote my own templates. Now I want to have the templeates files in my Plugin directory.

But that doenst work fine. Since I used the Plugin directory, wordpress doesent know that are my templates are wp templeates. What is the best way to let WP know, that these files are a wordpress Templates?

Thanks for answer :)

0

You would need to add a filter to the page_template hook that will look for whatever criteria you need.

For example, if your plugin was for a custom post type you would have something like this:

function myCustomTemplate( $page_template ) {

   // Check theme for template file
   $located = locate_template( 'single-{{postype}}' );

   if ( ! empty( $located ) && is_singular( '{{posttype}}' ) ) {
      $page_template = dirname( __FILE__ ) . 'templates/my-custom-template.php';
   }

   return $page_template;
}
add_filter( 'page_template', 'myCustomTemplate' );

The $located check looks to see if the user's current theme already has a file to display the custom post type already since you probably do not want to override that. If it doesn't and the post type is the custom post type your plugin defines, then it will tell WordPress to use the one with your plugin.

0

Thanks for your answer. It gives me a further idea where the problem might be. But somehow it doesent work out.

I will post my code, maybe you have any idea where the problem is.

add_action('template_redirect','check_access');
add_filter('page_template', 'plug_in_load_template');

function check_access() {     
    if ($_SESSION['login'] != 'ok') {
        wp_redirect(content_url('plugins/Login_Plugin/templates/index.php'));
        exit();
    }       
}

function plug_in_load_template() {
    $located = locate_template('single-{{postype}}');
    if (! empty($located) && is_singular('{{posttype}}')) {
        $page_template = plugin_dir_path( __FILE__ ) . 'Login_Plugin/templates/index.php';
    }
    return $page_template;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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