Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] すべての URL を相対 URL に置換する ( ただし OGP 内の画像等は、置換しない)
<?php
class relative_URI {
function relative_URI() {
add_action('get_header', array(&$this, 'get_header'), 1);
add_action('wp_footer', array(&$this, 'wp_footer'), 99999);
}
function replace_relative_URI($content) {
$home_url = trailingslashit(get_home_url('/'));
$parsed = parse_url($home_url);
$replace = $parsed['scheme'] . '://' . $parsed['host'];
$pattern = array(
'# (href|src|action)="'.preg_quote($replace).'([^"]*)"#ism',
"# (href|src|action)='".preg_quote($replace)."([^']*)'#ism",
);
$content = preg_replace($pattern, ' $1="$2"', $content);
$pattern = '#<(meta [^>]*property=[\'"]og:[^\'"]*[\'"] [^>]*content=|link [^>]*rel=[\'"]canonical[\'"] [^>]*href=|link [^>]*rel=[\'"]shortlink[\'"] [^>]*href=|data-href=|data-url=)[\'"](/[^\'"]*)[\'"]([^>]*)>#uism';
$content = preg_replace($pattern, '<$1"'.$replace.'$2"$3>', $content);
return $content;
}
function get_header(){
ob_start(array(&$this, 'replace_relative_URI'));
}
function wp_footer(){
ob_end_flush();
}
}
new relative_URI();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment