如何判断脚本是从WordPress中的插件,主题还是子主题执行的?

时间:2014-05-13 18:05:30

标签: php wordpress-plugin wordpress-theming wordpress

我有一个WordPress框架,可以嵌入到插件主题子主题中。为了返回正确的URL,脚本需要确定它的执行位置。

我相信我可以做一些匹配__FILE__的事情:

  • get_template_directory_uri()(主题),
  • get_stylesheet_directory_uri()(主题或儿童主题)或
  • plugin_dir_url( __FILE__ )(插件)

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

3 个答案:

答案 0 :(得分:2)

这个答案假定您实际上并不关心脚本文件是在插件,主题还是子主题中,而是想要找出您使用脚本包装的某些资产的正确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。例如,如果使用以下函数:

/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 ) )为{{1}}。

答案 1 :(得分:2)

此函数将返回一个字符串,指示从何处调用它。如果不在主题或插件中,则可能的返回值为pluginmu-pluginchild-themethemeFALSE

使用此功能时,请始终将__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()合并到一个函数中,该函数返回包含两个值的数组。

答案 2 :(得分:-1)

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

来源:Caller function in PHP 5?

我希望它可以帮到你!

相关问题