您提供的代码可能会导致不运行https的超链接中的第三方URL出现问题。您可以通过包含您的主页url来解决此问题,例如:
$content = str_replace( set_url_scheme( home_url(), \'http\' ), set_url_scheme( home_url(), \'relative\' ), $content);
接下来,当您想要显示内容时,您将应用此功能,这意味着您需要执行额外的步骤。也就是说,您需要应用一个名为
the_content
进行一些最终处理,如创建段落等:
$content = get_the_content();
$content = str_replace( set_url_scheme( home_url(), \'http\' ), set_url_scheme( home_url(), \'relative\' ), $content);
$content = apply_filters( \'the_content\', $content );
echo $content
最后,为了获得最大的兼容性,只需调用
the_content();
, 并使用
the_content
要进行修改的筛选器:
add_filter( \'the_content\', \'brandozz_url_filter\' );
function brandozz_url_filter( $content ) {
$content = str_replace( set_url_scheme( home_url(), \'http\' ), set_url_scheme( home_url(), \'relative\' ), $content);
return $content;
}
过滤器和挂钩可以放入插件或
functions.php
, 这是插件的外观:
/**
* Plugin Name: Relative local URLs
* Plugin URI: http://wordpress.stackexchange.com/questions/174228/remove-the-http-protocol-from-images
* Description: Replaces http:// URL containing the home url, with relative protocol urls
* Version: 1.0.0
* Author: Tom J Nowell
* Author URI: http://tomjn.com/
*/
add_filter( \'the_content\', \'tomjn_filter_relative_content_urls\' );
function tomjn_filter_relative_content_urls( $content ) {
$content = str_replace( set_url_scheme( home_url(), \'http\' ), set_url_scheme( home_url(), \'relative\' ), $content);
return $content;
}