WordPress 根据用户名/用户角色/能力/是否登录等隐藏部分文章内容

在写文章的时候,也许你需要添加一些只对特殊用户才显示的内容,今天就分享一下,通过简码来隐藏文章的部分内容,只有符合条件的用户(比如 指定的用户、属于某种用户角色、拥有某种能力、是否登录等)才能查看。

将下面的代码添加到主题的 functions.php:

/**
 * WordPress 根据用户名/用户角色/能力/是否登录等隐藏部分<a href="https://www.wpdaxue.com/tag/%e6%96%87%e7%ab%a0%e5%86%85%e5%ae%b9" title="查看与【文章内容】相关的文章" target="_blank" rel="noopener">文章内容</a>
 * https://www.wpdaxue.com/eyes-only-user-access-shortcode.html
 */
add_shortcode('eyesonly', 'sc_eyesonly');
function sc_eyesonly($atts, $content = null) {
	extract(shortcode_atts(array('username' =&gt; null, 'level' =&gt; null, 'logged' =&gt; null,'hide' =&gt; null,),$atts));
	if (!$hide || $hide === 'no' || $hide === 'false'){$pre = null;	$thecontent = do_shortcode($content);}
	if ($hide &amp;&amp; $hide !== 'no' &amp;&amp; $hide !== 'false'){$pre = do_shortcode($content); $thecontent = null;}
	$showcontent = $pre;
	$current_user = wp_get_current_user();
	$users = preg_split(&quot;/[\s,]+/&quot;,$username);
	$levels = preg_split(&quot;/[\s,]+/&quot;,$level);
	foreach($users as $name){if($username &amp;&amp; $current_user-&gt;user_login === $name){$showcontent = $thecontent;}}
	foreach($levels as $value){if($level &amp;&amp; current_user_can($value)){$showcontent = $thecontent;}}
	if($logged &amp;&amp; is_user_logged_in() &amp;&amp; $logged === 'in'){$showcontent = $thecontent;}
	if($logged &amp;&amp; !is_user_logged_in() &amp;&amp; $logged === 'out'){$showcontent = $thecontent;}
	return $showcontent;
}

以上代码添加了一个有多个参数可用的简码:

[eyesonly][/eyesonly]

参数为:

logged=&quot;in|out&quot;  //必填参数,登录/登出
level=&quot;anyrole, any_capability&quot;  //必填参数,可以填写角色或能力
username=&quot;anyusername&quot; // 必填参数,直接填写用户名
hide=&quot;yes&quot; // 选填参数,是否隐藏

注:前三个必填参数至少使用其中一个,当然可以两个,或三个一起用。

范例:

[eyesonly level=&quot;administrator, customrole, moderate_comments&quot;] 任何内容 [/eyesonly]

内容只有 ‘administrator’ 和自定义角色 ‘customrole’,以及拥有能力 ‘moderate_comments’ 的用户可见

[eyesonly hide=&quot;yes&quot; logged=&quot;out&quot; level=&quot;subscriber customrole&quot;] 任何内容 [/eyesonly]

内容对 未登录用户、以及 ‘subscriber’ 或 ‘customrole’ 这两种角色的用户都隐藏

[eyesonly username=&quot;joebob, billyjean&quot; level=&quot;administrator&quot;] 任何内容 [/eyesonly]

内容只有 administrator 这种角色的用户、以及用户名为 ‘joebob’ 和 ‘billyjean’ 的用户(不一定是 administrator 角色)可见

[eyesonly logged=&quot;in&quot;] 已登录用户可见 [/eyesonly]
[eyesonly logged=&quot;out&quot;] 未登录用户可见 [/eyesonly]

在同一个位置显示内容,对已登录和未登录用户显示不同内容

[eyesonly level=&quot;administrator&quot;] 任何内容 [/eyesonly]

只有 administrator 角色的用户可见

[eyesonly hide=&quot;yes&quot; level=&quot;administrator custom_capability&quot;] 任何内容 [/eyesonly]

内容对 administrator 角色的用户、以及拥有能力 ‘custom_capability’  的用户隐藏

代码出自:https://wordpress.org/plugins/eyes-only-user-access-shortcode/ 你可以访问该页面查看更多介绍。

来源:

https://www.wpdaxue.com/eyes-only-user-access-shortcode.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33160.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?