禁用未登录用户的开机自检图像

时间:2016-08-28 作者:Adam Robinsson

在用户登录之前,有没有什么好方法可以禁用某个帖子类别中的人物/图像。我希望整个帖子内容都是可见的,除了图像。

我不是程序员,也没有找到任何有用的插件。

2 个回复
最合适的回答,由SO网友:bynicolas 整理而成

您可以使用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

如果要替换图像,我将使用PHPDOMDocument 执行搜索和替换。

请注意,对于某些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;

}

SO网友:birgire

以下是如何为访客(未登录)删除带有标题的图像的建议:

add_filter( \'img_caption_shortcode\', function( $output, $attr, $content )
{
    $width = isset( $attr[\'width\'] ) ? $attr[\'width\'] : \'300\';
    $align = isset( $attr[\'align\'] ) ? $attr[\'align\'] : \'\';
    $class = isset( $attr[\'class\'] ) ? $attr[\'class\'] : \'no-image\';

    $class = sprintf( \'wp-caption %s %s\', $align, $class );

    return is_user_logged_in() 
        ? $output 
        : sprintf( 
            \'<div class="%s" width="%d">%s</div>\',
            esc_attr( $class ),
            (int) $width,
            esc_html__( \'Please log in to view the image\', \'mydomain\' )
        );
}, 10, 3 );
即,仅包装在[caption] 短代码将替换为文本。

标题的文本必须为非空,宽度必须为非零。

希望您能根据需要进行调整。

下面是一个示例:

no-image

相关推荐

致命错误:未捕获错误:无法将WP_ERROR类型的对象用作/../plugins/rm-payment.php中的数组

我使用2个WordPress站点、1个WordPress站点到2个WordPress站点的远程支付系统。第一个是主网站;第二个网站的工作方式类似于处理贝宝支付的商户网站。我们将第一个网站的用户订单详细信息提取到第二个网站,以处理贝宝付款。但在获取第二个网站的网页时出现错误,但请记住,如果重新加载它一次,问题就解决了致命错误:未捕获错误:无法将WP\\u error类型的对象用作/中的数组/插件/rm支付。php:第231行 $response = wp_remote_post( $remote_url,