So basically, I'm trying to check if the user wants to overwrite my plugin template in his theme.
I have a plugin with a Custom Post Type 'links'. According to WP template system my archive file is 'archive-links.php'.
So, why does my check for the file 'archive-links.php' in the template folder always return false?
if(is_post_type_archive('links')) {
$file = get_template_directory_uri() . '/archive-links.php';
if(file_exists($file)) {
// echo('working');
// template overwrite
return get_template_directory_uri() . '/archive-links.php';
} else {
// echo('not working');
// default plugin archive file
return plugin_dir_path(__DIR__) . 'templates/archive-links.php';
}
}
So basically, I'm trying to check if the user wants to overwrite my plugin template in his theme.
I have a plugin with a Custom Post Type 'links'. According to WP template system my archive file is 'archive-links.php'.
So, why does my check for the file 'archive-links.php' in the template folder always return false?
if(is_post_type_archive('links')) {
$file = get_template_directory_uri() . '/archive-links.php';
if(file_exists($file)) {
// echo('working');
// template overwrite
return get_template_directory_uri() . '/archive-links.php';
} else {
// echo('not working');
// default plugin archive file
return plugin_dir_path(__DIR__) . 'templates/archive-links.php';
}
}
get_template_directory_uri
doesn't return the directory name, it returns the URL of the directory. file_exists
needs a file path, not a file URL.
Use get_template_directory
instead, and don't forget to check get_stylesheet_directory
too for those using child themes
add_filter
call is missing as is the function name etc – Tom J Nowell ♦ Commented Jan 18, 2019 at 2:18