过滤includes_url()PHP函数(更改wp-includes URL)

时间:2012-06-29 16:23:37

标签: php wordpress

includes_url()是一个函数,用于检索WordPress中包含目录的URL,默认情况下,其输出类似于http://example.com/wp-includes/

该函数的code from the core

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 . '/';

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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()