How to determine if the current file is loaded in a plugin or in a theme? A boolean PHP function.
3 Answers
EDIT
Talking to @s_ha_dum, there are issues between operating system with the paths, Windows uses backslashes, so we need to compensate for them
Because OP needs a boolean returned, we need a function to return either true or false, so we can either write two separate functions to cover both bases or just choose one and go with it. This method has its drawbacks though as it does not cover for files loaded outside the plugins and theme folders
Lets look at a conditional that would return true if a file is loaded from a theme
function is_file_in_theme( $file_path = __FILE__ )
{
// Get the them root folder
$root = get_theme_root();
// Change all '\' to '/' to compensate for localhosts
$root = str_replace( '\\', '/', $root ); // This will output E:\xampp\htdocs\wordpress\wp-content\themes
// Make sure we do that to $file_path as well
$file_path = str_replace( '\\', '/', $file_path );
// We can now look for $root in $file_path and either return true or false
$bool = stripos( $file_path, $root );
if ( false === $bool )
return false;
return true;
}
We can alternatively do the same for plugins
function is_file_in_plugin( $file_path = __FILE__ )
{
// Get the plugin root folder
$root = WP_PLUGIN_DIR;
// Change all '\' to '/' to compensate for localhosts
$root = str_replace( '\\', '/', $root );
// Make sure we do that to $file_path as well
$file_path = str_replace( '\\', '/', $file_path );
// We can now look for $root in $file_path and either return true or false
$bool = stripos( $file_path, $root );
if ( false === $bool )
return false;
return true;
}
Interesting question.
The content directory and the plugins directory can be altered but I don't believe you can alter the "themes" portion of the wp-content
directory, so:
function theme_or_plugin_wpse_213043($path) {
$path = str_replace('\\','/',$path);
$path = explode('/',$path);
return in_array('themes',$path);
}
var_dump(theme_or_plugin_wpse_213043(__FILE__));
The function will return
true
for theme files and false
otherwise-- this inlcudes plugins and mu-plugins.
I am also fairly certain that if you need this function you are doing something else incorrectly-- probably using the wrong methods to get the theme/plugin paths or URLs.
1. The function:
function wp543_is_plugin(){
return strpos( str_replace("\\", "/", plugin_dir_path( $file ) ) , str_replace("\\", "/", WP_PLUGIN_DIR) ) !== false;
}
2. Call the function:
wp543_is_plugin();
Calling this function will return true
if the current file is loaded in a plugin and false
if it is loaded in a theme.
Edit: Since this answer has received many downvotes I've found the solution that should fix my previous mistake. And I intentionally solved it with just one line of code. :)
How this function works now:
- Replaces all backslashes wit forward slashes.
plugin_dir_path( __FILE__ )
will always return the full path to current file. No matter if thatfile is within a plugin or theme.WP_PLUGIN_DIR
will always return the plugins path, so we just check if that path is found inplugin_dir_path( __FILE__ )
- If it is found than the current file is within a plugin and return
true
otherwisefalse
.