如何判断一个脚本是由插件、主题还是子主题执行的


How to tell if a script is being executed from a plugin, a theme or a child theme in WordPress?

我有一个WordPress框架,可以嵌入到插件主题子主题。为了返回正确的url,脚本需要确定从哪里执行。

我相信我可以做一些事情,比如匹配__FILE__与:

  • get_template_directory_uri() (theme),
  • get_stylesheet_directory_uri()(主题或子主题),或
  • plugin_dir_url( __FILE__ ) (plugin)

有没有更好、更可靠的方法?无论如何,我该怎么做呢?

此函数将返回一个字符串,指示从何处调用该函数。如果不在主题或插件中,可能的返回值为plugin, mu-plugin, child-theme, themeFALSE

使用此函数时,始终传递__DIR__作为参数,例如:where_am_i(__DIR__) .

/**
 * @param string $directory always __DIR__
 */
function where_am_i($directory) {
    $current_directory = forward_slashes($directory);
    $plugins_directory = forward_slashes(WP_PLUGIN_DIR);
    $mu_plugins_directory = forward_slashes(WPMU_PLUGIN_DIR);
    $themes_directory = forward_slashes(get_theme_root());
    if ( strpos ( $current_directory, $plugins_directory ) !== FALSE ) {
        $location = 'plugin';
    } elseif ( strpos ( $current_directory, $mu_plugins_directory ) !== FALSE ) {
        $location = 'mu-plugin';
    } elseif ( strpos ( $current_directory, $themes_directory ) !== FALSE ) {
        // Script is in a theme, determine if parent or child
        $stylesheet_directory = forward_slashes(get_stylesheet_directory());
        if ( is_child_theme() && ( strpos ( $current_directory, $stylesheet_directory ) !== FALSE ) ) {
            $location = 'child-theme';
        } else {
            $location = 'theme';
        }
    } else {
        // not in a theme or plugin
        $location = FALSE;
    }
    return $location;
}
/**
 * Handle Windows paths
 */
function forward_slashes($string) {
    return str_replace('''', '/', $string);
}

下面的函数where_am_i_dir()将给出调用它的位置的目录结构,但省略*/wp-content/{$subdir}。如果它是从/wp-content/themes/twentyfourteen/libs/script.php调用的,它将返回/twentyfourteen/libs(如果你想让它返回一个尾斜杠,你可以在斜杠被过滤时将一个连接到$directory)。

第一个参数是where_am_i()的返回值,第二个参数始终是__DIR__。示例:where_am_i_dir($location, __DIR__) .

/**
 * @param string $location return from where_am_i()
 * @param string $directory always __DIR__
 */
function where_am_i_dir($location, $directory) {
    if ($location == 'plugin') {
        $subdirectory_name = '/plugins';
    } elseif ($location == 'mu-plugin') {
        $subdirectory_name = '/mu-plugins';
    } elseif ($location == 'theme' || $location == 'child-theme') {
        $subdirectory_name = '/themes';
    } else {
        return FALSE;
    }
    $directory = forward_slashes($directory);
    $wp_content_directory = forward_slashes(WP_CONTENT_DIR) . $subdirectory_name;
    return str_replace($wp_content_directory, '', $directory);
}

如果您感兴趣,您可以将where_am_i()where_am_i_dir()组合成一个函数,该函数返回具有两个值的数组。

这个答案假设你实际上并不关心脚本文件是在插件、主题还是子主题中,而是想要找出一些与脚本打包在同一目录或子目录中的资产的正确URL。

function proper_url($directory) {
    $current_directory = forward_slashes($directory);
    $plugins_directory = forward_slashes(WP_PLUGIN_DIR);
    $themes_directory = forward_slashes(get_theme_root());
    if ( strpos( $current_directory, $plugins_directory ) !== FALSE ) {
        // Script is in a plugin
        $dir = str_replace( $plugins_directory, '', $current_directory);
        $url = plugins_url() . $dir;
    } elseif ( strpos ( $current_directory, $themes_directory ) !== FALSE ) {
        // Script is in a theme
        $dir = str_replace( $themes_directory, '', $current_directory);
        $url = get_theme_root_uri() . $dir;
    } else {
        // If needed, do error handling here
        $url = FALSE;
    }
    return $url;
}
/**
 * Handle Windows paths
 */
function forward_slashes($string) {
    return str_replace('''', '/', $string);
}

使用proper_url(__DIR__)将返回调用该函数的脚本所在的URL。例如,如果使用from函数:

/wp-content/themes/my-awesome-theme/my-framework/framework.php

将返回:

http://www.example.com/wp-content/themes/my-awesome-theme/my-framework/

你会注意到我使用常数WP_PLUGIN_DIR而不是plugin_dir_path(),因为后者只是trailingslashit( dirname( $file ) )的包装。

参见php函数http://php.net/manual/en/function.debug-backtrace.php

来源:调用函数在PHP 5?

我希望它能帮助你!