私は自宅とサイトのURLとしてipv6 ip addressを設定しようとしましたが、それは完全には動作しないようです。
そこから:http://[2001:da8:d800:71::250]/wordpress/
(ソースを見れば)pingback
(16行目)のようないくつかのURLが正しいが、rss
(20行目)のような他のURLが[
と]
で失われた、どうすれば解決できますか? (私がこのipv6アドレスを指すドメインを持っていない場合)
私のここでの問題は、wp-coreで使われているURLの多くをサニタイズするのに使われる esc_url() 関数であると思います。
format.php の関数定義を見ると、2627行目の正規表現が[
と]
を除外していることがわかります。
しかし幸いなことに、2656行目にこの振る舞いを上書きできるようにするフィルタが用意されていることもわかります。そのため、clean_url
フィルタにカスタム関数を添付することでこれを修正できます。
**EDIT2**
リダイレクトURLは別の機能でサニタイズされているようです。幸いこれは プラグイン可能な関数 なので、上書きすることができます。これを考慮してコードを編集しました。 mu-plugins
フォルダー内にwp-content
フォルダーを作成し、そのフォルダー内にPHPファイル(私はmine fix_ipv6_siteurl.php
と名前を付けます)を配置することで、次のコードスニペットを使用できます。
<?php
add_filter('clean_url', 'wpse_120978_custom_esc_url', 10, 3);
function wpse_120978_custom_esc_url( $url, $original_url, $_context ) {
if( !strpos($original_url, '[') ) {
return $url;
}
$url = $original_url;
if ( '' == $url )
return $url;
$url = preg_replace('|[^a-z0-9-[]~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
$strip = array('%0d', '%0a', '%0D', '%0A');
$url = _deep_replace($strip, $url);
$url = str_replace(';//', '://', $url);
/* If the URL doesn't appear to contain a scheme, we
* presume it needs http:// appended (unless a relative
* link starting with / or a php file).
*/
if ( strpos($url, ':') === false &&
substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
$url = 'http://' . $url;
// Replace ampersands and single quotes only when displaying.
if ( 'display' == $_context ) {
$url = wp_kses_normalize_entities( $url );
$url = str_replace( '&', '&', $url );
$url = str_replace( "'", ''', $url );
}
if ( !is_array($protocols) )
$protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'Gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn');
if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
return '';
return $url;
}
if ( !function_exists('wp_sanitize_redirect') ) :
/**
* Sanitizes a URL for use in a redirect.
*
* @since 2.3
*
* @return string redirect-sanitized URL
**/
function wp_sanitize_redirect($location) {
$location = preg_replace('|[^a-z0-9-[]~+_.?#=&;,/:%!]|i', '', $location);
$location = wp_kses_no_null($location);
// remove %0d and %0a from location
$strip = array('%0d', '%0a', '%0D', '%0A');
$location = _deep_replace($strip, $location);
return $location;
}
endif;
(functions.phpからの前のコードは削除することができます。)