您可以使用PHPstrip_tags 并连接到the_content 通过检查用户是否is logged in.
将此添加到functions.php 文件
add_filter( \'the_content\', \'wpse_remove_img\' );
function wpse_remove_img( $content ){
if( ! is_user_logged_in() ){
// You need to specify which tag you want to keep here it\'s p, a, h2,span, div.
// Adjust accordingly (you might want to add other or remove all)
$content = strip_tags($content, \'<p><a><h2><span><div>\');
}
return $content;
}
EDIT
如果要替换图像,我将使用PHP
DOMDocument 执行搜索和替换。
请注意,对于某些HTML5标记,可能会遇到一些文本编码问题或警告。Hakre 深入解释为什么会发生这种情况,以及Gordon 很好地解释了这一点,都在StackExchange。
在下面的解决方案中,我通过在$content 已加载。以及临时禁用libxml错误,这样我们就不会因为使用libxml_use_internal_errors
再一次,这会进入你的functions.php
add_filter( \'the_content\', \'wpse_replace_img\' );
function wpse_replace_img( $content ){
if( ! is_user_logged_in() ){
$dom = new DOMDocument();
libxml_use_internal_errors(true); // deactivating errors
// Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
$dom->loadHTML( \'<?xml encoding="UTF-8">\' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
libxml_use_internal_errors(false); // reactivating errors
$tags = $dom->getElementsByTagName( \'img\' );
foreach ($tags as $img) {
$new_src_url = \'http://url/to/your/filler-image.jpg\';
$img->setAttribute( \'src\', $new_src_url );
$img->setAttribute( \'srcset\', \'\' );
}
$content = $dom->saveHTML();
}
return $content;
}
EDIT
好的,这里有一个考虑到包装的编辑
<a> 图像周围的标记。第一个解决方案将用
#add_filter( \'the_content\', \'wpse_replace_img\' );
function wpse_replace_img( $content ){
if( ! is_user_logged_in() ){
$dom = new DOMDocument();
libxml_use_internal_errors(true);
// Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
$dom->loadHTML( \'<?xml encoding="UTF-8">\' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
libxml_use_internal_errors(false);
$tags = $dom->getElementsByTagName( \'img\' );
foreach ($tags as $img) {
$new_src_url = \'http://url/to/your/filler-image.jpg\';
$img->setAttribute( \'src\', $new_src_url );
$img->setAttribute( \'srcset\', \'\' );
if( $img->parentNode->tagName == \'a\' ){ // if the current image is wrapped by an <a> tag, replace href link with an #
$img->parentNode->setAttribute( \'href\', \'#\' );
}
}
$content = $dom->saveHTML();
}
return $content;
}
然后您还可以删除
<a> 全部标记。在以下人员的帮助下
matb33 在stackoverflow,我设法想出了这个解决方案。
add_filter( \'the_content\', \'wpse_replace_img\' );
function wpse_replace_img( $content ){
if( ! is_user_logged_in() ){
$dom = new DOMDocument();
libxml_use_internal_errors(true);
// Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
$dom->loadHTML( \'<?xml encoding="UTF-8">\' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
libxml_use_internal_errors(false);
$tags = $dom->getElementsByTagName( \'img\' );
$fragment = $dom->createDocumentFragment(); // Create our new document fragment to manipulate
foreach ($tags as $img) {
$new_src_url = \'http://url/to/your/filler-image.jpg\';
$img->setAttribute( \'src\', $new_src_url );
$img->setAttribute( \'srcset\', \'\' );
if( $img->parentNode->tagName == \'a\' ){ // if the current image has an <a> tag as a parent apply replace our <a> tag with our <img> tag
$a = $img->parentNode;
$fragment->appendChild( $img->parentNode->childNodes->item( 0 ) ) ; // store our image node into the fragment
$a->parentNode->replaceChild( $fragment, $a ); // replace our <a> tag with our <img> tag
}
}
$content = $dom->saveHTML();
}
return $content;
}
EDIT
下面是如何仅适用于特定类别
add_filter( \'the_content\', \'wpse_replace_img\' );
function wpse_replace_img( $content ){
if( ! is_user_logged_in() ){
$apply_restriction = false;
$categories = get_the_category();
$restricted_cat = array(
\'restricted_cat_slug\' // Our restricted category slug, since it\'s an array you could provide more than one
);
// Performing our check, switch the flag if we find a match
foreach( $categories as $cat ){
foreach( $restricted_cat as $r_cat ){
if( $cat->slug == $r_cat ){
$apply_restriction = true;
}
}
}
// Bail out without applying any restriction if no category match is found
if( ! $apply_restriction ){
return $content;
}
$dom = new DOMDocument();
libxml_use_internal_errors(true);
// Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
$dom->loadHTML( \'<?xml encoding="UTF-8">\' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
libxml_use_internal_errors(false);
$tags = $dom->getElementsByTagName( \'img\' );
$fragment = $dom->createDocumentFragment(); // Create our new document fragment to manipulate
foreach ($tags as $img) {
$new_src_url = \'http://url/to/your/filler-image.jpg\';
$img->setAttribute( \'src\', $new_src_url );
$img->setAttribute( \'srcset\', \'\' );
if( $img->parentNode->tagName == \'a\' ){ // if the current image has an <a> tag as a parent apply replace our <a> tag with our <img> tag
$a = $img->parentNode;
$fragment->appendChild( $img->parentNode->childNodes->item( 0 ) ) ; // store our image node into the fragment
$a->parentNode->replaceChild( $fragment, $a ); // replace our <a> tag with our <img> tag
}
}
$content = $dom->saveHTML();
}
return $content;
}