0

includes_url()是一个在 WordPress 中检索包含目录的 url 的函数,默认情况下其输出看起来像http://example.com/wp-includes/.

该函数的核心代码

function includes_url($path = '') {
    $url = site_url() . '/' . WPINC . '/';

    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
        $url .= ltrim($path, '/');

    return apply_filters('includes_url', $url, $path);
}

如何用我自己的函数替换它(使用functions.php)?本质上,我想将第二行更改为 -$url = 'http://static-content.com/' . WPINC . '/';

4

2 回答 2

4

您可以使用一个过滤器add_filter来使现有函数返回想要的内容:

$callback = function($url, $path) {
    $url = 'http://static-content.com/' . WPINC . '/';

    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
        $url .= ltrim($path, '/');

    return $url;
};

add_filter('includes_url', $callback, 10, 2);

编辑: PHP 5.2 版本:

function includes_url_static($url, $path) {
    $url = 'http://static-content.com/' . WPINC . '/';

    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
        $url .= ltrim($path, '/');

    return $url;
}

$callback = 'includes_url_static';

add_filter('includes_url', $callback, 10, 2);
于 2012-06-29T17:36:03.477 回答
0

一种选择是创建您自己的函数并让它调用includes_url()和更改它。

function custom_includes_url($path = '') {
  $url = includes_url($path);

  return str_replace(site_url(), 'http://static-content.com', $url);
}

但是你必须在custom_includes_url()任何地方调用。

于 2012-06-29T17:31:31.300 回答