2012-06-29 23 views
0

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

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

回答

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()

+0

不幸的是每插件使用'includes_url()',所以我需要使用函数中的过滤器来修改它以满足我的需要。如果问题只出现在我的主题上,答案将是一种选择。但是非常感谢你们的帮助。 :) –

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); 
+0

第一行是否正确?我在添加代码时遇到了这个错误:'解析错误:语法错误,在657行/home/xxxx/public_html/wp-content/themes/bbbb/functions.php中出现意外的T_FUNCTION' –

+0

这是因为你的PHP版本是不兼容。您需要先定义函数,然后将其名称作为字符串添加。我加了一个关于这个的例子。 – hakre

+0

+1不知道WordPress的做到了这一点 –