I am working on a plugin. In plugin folder, there is a folder 'template' from where i copied the files and paste into 'my_folder' in active theme. They move perfectly. Now when i editing files from the theme they did not edited, but when i edit files from plugin folder, they edited.

I just want that when i edit files from theme the result will show on frontend, not from plugin.

I also use template_include hook but i don't know where i go wrong.

here is my code:

add_filter('template_include', 'my_template_chooser');
function my_template_chooser($template){
    global $wp_query;
    $plugindir = dirname(__FILE__);

    $post_type = get_query_var('post_type');

    if( $post_type == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';

    if( $post_type == 'my_items' && is_single() ){
        if(file_exists(TEMPLATEPATH . '/my_items/single-my_items.php')) {
            return locate_template('my_items/single-my_items.php');
        } else {
            return $plugindir . '/template/single-my_items.php';

    if (is_tax('my_items_tags')) {
        if(file_exists(TEMPLATEPATH . '/my_items/taxonomy-my_items_tags.php')) {
            return locate_template('my_items/taxonomy-my_items_tags.php');
        } else {
            return $plugindir . '/template/taxonomy-my_items_tags.php';

    return $template;   

I also try this

return locate_template('my_items/taxonomy-my_items_tags.php');


return TEMPLATEPATH .'my_items/taxonomy-my_items_tags.php';

But nothing happen. Please tell me where i am doing wrong

1 Answer 1


To get the template directory, you can use get_template_directory_uri() like this:

$themeDir = get_template_directory_uri();

// ...
$themeItems = $themeDir . '/my_folder/my_items.php';
if (file_exists($themeItems))
  return $themeItems;
  return $pluginDir . '/template/my_items.php';

For more info visit Function Reference/get template directory uri.. If you want to place your PHP code in your theme directory, the better place for that is the "functions.php" file, or include($theFile) in the "functions.php" where you want to place your PHP code.

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.