我跟踪了我的主题(Chip Bennett的Oenology子主题)和生成链接的wp include中的函数调用,发现home_url()
函数,然后为我的站点编写以下函数:
function gregory_make_relative($url=\'\') {
return preg_replace( \'#^https?://[^/]+/#iu\', \'/\', $url, 1 );
}
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );
我故意在域后搜索grep以包含/以便我仍然可以使用
home_url(\'\')
返回博客的指定域,并在
<head>
通过WordPress SEO使用以下功能(即,无论用于加载页面的域是什么,规范链接都是相同的;即,没有“重复内容”):
function gregory_wpseo_canonical_add_domain( $canonical ) {
return home_url(\'\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );
到目前为止,它运行得非常好,但我想知道它是否会对feed或我最终实现的电子商务解决方案产生负面影响。欢迎评论、注释、提示和警告:-)
更新一个简单的home_url()
(即,未指定路径)在整个系统中使用,因此无法使用grep搜索中的尾随/。我不得不删除它,并找到另一种方法来指定规范url中的域。因此,通过wp进行的更多研究包括,现在函数如下所示:
function gregory_make_relative($url=\'\') {
return preg_replace( \'#^https?://[^/]+#iu\', \'\', $url, 1 );
}
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );
function gregory_wpseo_canonical_add_domain( $canonical ) {
// get_option() is defined in wp-includes/functions.php and is used by get_home_url()
return get_option(\'home\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );
更新2
比最初出现的更难;-)我的代码现在看起来像这样。馈送受到影响
在生成链接的函数链中的某个位置,提要似乎正在使用\\u服务器[\'HTTP\\U主机\']我必须在那里检查我的选择。
/* FILTERS TO PRODUCE ROOT-RELATIVE URLs */
// define WP_SITEURL because the formula in wp-includes/functions::wp_guess_url() makes a
// false assumption and appends $_SERVER[\'REQUEST_URI\'] to the base_url.
define(\'WP_SITEURL\', \'http://my.domain.hk/\', true );
// strip the domain
function gregory_make_relative( $url=\'\' ) {
return preg_replace( \'#^https?://[^/]+#iu\', \'\', $url, 1 );
}
add_filter( \'site_url\', \'gregory_make_relative\', 11, 1 );
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );
add_filter( \'template_directory_uri\', \'gregory_make_relative\', 11, 1 );
add_filter( \'stylesheet_directory_uri\', \'gregory_make_relative\', 11, 1 );
add_filter( \'script_loader_src\', \'gregory_make_relative\', 11, 1 );
function gregory_make_stylehref_relative( $tag=\'\' ) {
// $wp_styles->do_item() passes this along to the filter:
// "<link rel=\'$rel\' id=\'$handle-rtl-css\' $title href=\'$rtl_href\' type=\'text/css\' media=\'$media\' />\\n"
$matches = array();
if( !preg_match( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', $tag, &$matches ))
return $tag;
$matches[2] = gregory_make_relative($matches[2]);
return $matches[1].$matches[2].$matches[3];
}
add_filter( \'style_loader_tag\', \'gregory_make_stylehref_relative\', 11, 1 );
function gregory_wpseo_canonical_add_domain( $canonical=\'\' ) {
// get_option is defined in wp-includes/functions.php and is used by get_home_url() to get the home url.
return get_option(\'home\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );
更新3——WPSEO中有一个选项,可以在RSS帖子前后添加文本/链接,包括使用占位符的选项。其中一个占位符是%%BLOGLINK%%
. 不幸的是,在根相对筛选器就绪的情况下,%%BLOGLINK%%生成了一个空字符串,该字符串在提要中没有用处。这段代码解决了这个问题(注意,我的另一个选择是简单地在WPSEO的RSS设置中硬编码链接,这可能是更明智的做法:-)
// a change for WPSEO\'s %%BLOGLINK%% code.
// I changed get_bloginfo() to get_bloginfo_rss() in wpseo/frontend/class-frontend.php to allow this.
// without these changes, %%BLOGLINK%% is printed into the rss feeds as an empty string.
function gregory_set_domain_in_rss_urls( $info, $show ) {
// copied from wp-includes/general-template.php::get_bloginfo()
$url = true;
if (strpos($show, \'url\') === false &&
strpos($show, \'directory\') === false &&
strpos($show, \'home\') === false)
$url = false;
return ( !$url || !empty($info) ? $info : \'/\' );
}
add_filter( \'get_bloginfo_rss\', \'gregory_set_domain_in_rss_urls\', 11, 2 );
(我决定在发布消息后对RSS进行硬编码,因此在我的主题中禁用了上述过滤器。)
更新4--preg\\u replace()变为preg\\u match()
我已经更新了
gregory_make_stylehref_relative()
更新2中显示的要使用的函数
preg_match()
而不是
preg_replace()
. 这是旧代码:
$href = preg_replace( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', \'$2\', $tag );
$href = gregory_make_relative($href);
return preg_replace( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', \'$1\'.$href.\'$3\', $tag );
更新5——get\\u avatar()
另一个过滤器,这次是get\\u avatar(),以便模式和当前域包含在wp的空白路径中。gif。如果没有这些,将无法找到和加载gif。如果没有该模式,Gravatar将加载自己的公司头像。
function gregory_avatar_add_domain( $avatar ) {
// look for urlencode( includes_url(\'images/blank.gif\')) in the $avatar string.
// if found, encode the schema and domain, insert it into the $avatar string.
$gif = includes_url(\'images/blank.gif\'); // from get_avatar()
if( preg_match( \'|^https?://|i\', $gif ))
// the url already includes the schema (and domain).
return $avatar;
$gif = urlencode($gif);
$schema = is_ssl() ? \'https://\' : \'http://\'; // from wp_guess_url()
$domain = urlencode( $schema . $_SERVER[\'HTTP_HOST\'] );
return str_replace( $gif, $domain.$gif, $avatar );
}
add_filter( \'get_avatar\', \'gregory_avatar_add_domain\', 11, 1 );
更新6—redirect\\u canonical()
筛选器导致涉及查询字符串的传入url出现问题。url的
scheme://domain
会被砍成
://domain
. 花了几个小时才找到问题,但它在wp includes/canonical中。redirect\\u canonical()和那里的一个过滤器挂钩可以纠正这个问题。以下是过滤器:
function gregory_redirect_canonical_addScheme( $redirect_url ) {
// redirect_canonical() requires (but doesn\'t need) a fully qualified url.
// get_permalink() in the second iteration of redirect_canonical()
// (redirect_canonical() calls itself) usually switches to the domain specified in the
// WP Settings. but if we do the same, redirect_canonical() doesn\'t recognise the
// permalink as a redirect, and WP doesn\'t update the url in the User Agent\'s Address bar.
if( preg_match( \'|^https?://|i\', $redirect_url ))
// fully qualified url. leave it alone.
return $redirect_url;
if( substr( $redirect_url, 0, 3 ) == \'://\' )
// no scheme specified in $redirect during the
// second pass through redirect_canonical().
return (is_ssl() ? \'https\' : \'http\') . $redirect_url;
if( substr( $redirect_url, 0, 1 ) == \'/\' )
// root-relative url.
return (is_ssl() ? \'https://\' : \'http://\').$_SERVER[\'HTTP_HOST\'].$redirect_url;
// relative url. not root-relative.
return (is_ssl() ? \'https://\' : \'http://\').$_SERVER[\'HTTP_HOST\'].\'/\'.$redirect_url;
}
add_filter( \'redirect_canonical\', \'gregory_redirect_canonical_addScheme\', 11, 1 );
请注意,我还没有将我的问题标记为刚才的答案,因为我不知道这些过滤器在今后会产生什么后果。需要更多的时间和测试。
干杯,格里高利(WordPress 3.3.2)