1

How to determine if the current file is loaded in a plugin or in a theme? A boolean PHP function.

0

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;
}
-1

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.

  • This wouldn't work if your plugin had a themes folder that you were checking. – jgraup Jan 4 '16 at 19:26
  • That's true jgraup. I'm sure there are other ways to break it too – s_ha_dum Jan 4 '16 at 19:51
  • To your point though, we don't know why the OP needs this function so it's hard to determine best vs. better. – jgraup Jan 4 '16 at 20:02
-1

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:

  1. Replaces all backslashes wit forward slashes.
  2. plugin_dir_path( __FILE__ ) will always return the full path to current file. No matter if thatfile is within a plugin or theme.
  3. WP_PLUGIN_DIR will always return the plugins path, so we just check if that path is found in plugin_dir_path( __FILE__ )
  4. If it is found than the current file is within a plugin and return true otherwise false.
  • For anyone who downvote this post, please add arguments. – Andrei Surdu Feb 17 '16 at 22:57

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.