决定接受这个想法。改编自我的片段changes anchors to collapsed domain names. 有点过于冗长,但似乎有效。
add_filter( \'the_content\', \'anchors_to_page_titles\' );
function anchors_to_page_titles( $content ) {
preg_match_all( \'/<a.*?href="(.*?)".*?>(.*?)<\\/a>/\', $content, $matches );
array_shift( $matches );
foreach( $matches[0] as $key => $url ) {
$anchor = $matches[1][$key];
if( $url == $anchor ) {
$transient_key = \'page_title_\'.md5($url);
$anchor = get_transient($transient_key);
if( !$anchor ) {
$response = wp_remote_request($url);
$body = wp_remote_retrieve_body($response);
$pattern = \'/title>(.*?)</\';
$title = array();
preg_match( $pattern, $body, $title);
if( !empty( $title ) ) {
$title = $title[1];
$anchor = $title;
set_transient( $transient_key, $anchor, 60*60*24 );
}
else {
$anchor = $url;
set_transient( $transient_key, $anchor, 60*60 );
}
}
$content = str_replace( ">{$url}</a>", ">{$anchor}</a>", $content );
}
}
return $content;
}
PS也许在保存时修改帖子比在显示时过滤更有意义。。。嗯,根据我的评论,我不认为页面标题对这有好处。