教程:如何简易快速隐藏文章内容

隐藏内容现在很流行。无论是收费还是单独为了隐藏内容给会员等等,都是增加用户粘性的一种方法。我们这里介绍下一种简易的隐藏方法。它方便快捷。当然你也可以根据这个思路增加自己的特殊功能。但原理是一样的。那就是短码。不多说话。你只需要把下面的代码放在您的主题的function.php文件里就成了。然后在编辑文章时使用相关的短码即可。

具体代码如下:

短码[ie_guest] 这里是隐藏内容[/ie_guest]

add_shortcode( 'ie_guest', 'ie_guest_sc' );
function ie_guest_sc( $atts, $content = null ) {
    extract( shortcode_atts( array(
               'in' => '',
               'admin' => '',
    ), $atts ) );
    $in = str_replace(' ', '', $in);
    $includeds = explode(",", $in);

    $user_id = get_current_user_id();

    //check if user is logged in
    if ( is_user_logged_in() ) {
        //check if admin is allowed
        if ($admin === '1') {
            //loop through included user ids
            foreach ($includeds as $included) {
                //check if user is included
                if ($user_id == $included) {
                    return do_shortcode($content);
                }
            }
            //check if user is admin
            if ( current_user_can('administrator') ) {
                return do_shortcode($content);
            } else {
                return '';
            }
        } else {
            //loop though included user ids
            foreach ($includeds as $included) {
                //check if user is included
                if ($user_id == $included) {
                    return do_shortcode($content);
                }
            }
            return '';
        }
    //show content to guests
    } else {
        return do_shortcode($content);
    }
}

短码[ie_loggedin] 这里是隐藏内容[/ie_loggedin]

add_shortcode( 'ie_loggedin', 'ie_loggedin_sc' );
function ie_loggedin_sc( $atts, $content = null ) {
    extract( shortcode_atts( array(
      'ex' => '',
    ), $atts ) );

    $ex = str_replace(' ', '', $ex);
    $excludeds = explode(",", $ex);

    $user_id = get_current_user_id();

    //check if user is logged in
    if ( is_user_logged_in() ) {
        //loop through excluded user ids
        foreach ($excludeds as $excluded) {
            //check if user is excluded
            if ($user_id == $excluded) {
                //show nothing
                return '';
            }
        }
        //show content to logged in users
        return do_shortcode($content);
    //hide content to guests
    } else {
        return '';
    }
}

短码[ie_role roles="administrator, editor"] 这里是隐藏内容[/ie_role],这里的roles指的是您的用户组。

add_shortcode( 'ie_role', 'ie_role_sc' );
function ie_role_sc( $atts, $content = null ) {
  
  extract( shortcode_atts( array(
    'roles' => '',
    'inverse' => '',
  ), $atts ) );

  if (!is_user_logged_in()) { return; }

  $roles = str_replace(' ', '', $roles);
  $role_array = explode(",", $roles);

  $user = wp_get_current_user();
  $user_roles = ( array ) $user->roles;

  //loop through allowed roles
  if ($inverse !== '1') {
    foreach($user_roles as $user_role) {
      foreach ($role_array as $allowed_role) {
        //check if user has allowed role
        if ($user_role == $allowed_role) {
          //show nothing
          return do_shortcode($content);
        }
      }
    }
  } else {
    $role_found = 0;
    foreach($user_roles as $user_role) {
      foreach ($role_array as $allowed_role) {
        //check if user has allowed role
        if ($user_role == $allowed_role) {
          //show nothing
          $role_found = 1;
        }
      }
    }
    if (!$role_found) {
      return do_shortcode($content);
    }
  }
  
  return;

}

本文已在Ie主题99839发布

文章来源:https://ietheme.com/restrict-content.html


撰写评论

您的电子邮箱地址不会被公开。 必填项已用*标注

加入我们

注册完成!

密码重置

请输入您的邮箱地址。 您将收到一个链接来创建新密码。

检查你的邮件中的确认链接。