在寫文章的時候,也許你需要添加一些只對特殊用戶才顯示的內(nèi)容,今天就分享一下,通過簡碼來隱藏文章的部分內(nèi)容,只有符合條件的用戶(比如 指定的用戶、屬于某種用戶角色、擁有某種能力、是否登錄等)才能查看。
將下面的代碼添加到主題的 functions.php:
/**
* WordPress 根據(jù)用戶名/用戶角色/能力/是否登錄等隱藏部分文章內(nèi)容
* http://www.ydqwiac.cn/eyes-only-user-access-shortcode.html
*/
add_shortcode('eyesonly', 'sc_eyesonly');
function sc_eyesonly($atts, $content = null) {
extract(shortcode_atts(array('username' => null, 'level' => null, 'logged' => null,'hide' => null,),$atts));
if (!$hide || $hide === 'no' || $hide === 'false'){$pre = null; $thecontent = do_shortcode($content);}
if ($hide && $hide !== 'no' && $hide !== 'false'){$pre = do_shortcode($content); $thecontent = null;}
$showcontent = $pre;
$current_user = wp_get_current_user();
$users = preg_split("/[\s,]+/",$username);
$levels = preg_split("/[\s,]+/",$level);
foreach($users as $name){if($username && $current_user->user_login === $name){$showcontent = $thecontent;}}
foreach($levels as $value){if($level && current_user_can($value)){$showcontent = $thecontent;}}
if($logged && is_user_logged_in() && $logged === 'in'){$showcontent = $thecontent;}
if($logged && !is_user_logged_in() && $logged === 'out'){$showcontent = $thecontent;}
return $showcontent;
}
以上代碼添加了一個有多個參數(shù)可用的簡碼:
[eyesonly][/eyesonly]
參數(shù)為:
logged="in|out" //必填參數(shù),登錄/登出
level="anyrole, any_capability" //必填參數(shù),可以填寫角色或能力
username="anyusername" // 必填參數(shù),直接填寫用戶名
hide="yes" // 選填參數(shù),是否隱藏
注:前三個必填參數(shù)至少使用其中一個,當(dāng)然可以兩個,或三個一起用。
范例:
[eyesonly level="administrator, customrole, moderate_comments"] 任何內(nèi)容 [/eyesonly]
內(nèi)容只有 ‘administrator’ 和自定義角色 ‘customrole’,以及擁有能力 ‘moderate_comments’ 的用戶可見
[eyesonly hide="yes" logged="out" level="subscriber customrole"] 任何內(nèi)容 [/eyesonly]
內(nèi)容對 未登錄用戶、以及 ‘subscriber’ 或 ‘customrole’ 這兩種角色的用戶都隱藏
[eyesonly username="joebob, billyjean" level="administrator"] 任何內(nèi)容 [/eyesonly]
內(nèi)容只有 administrator 這種角色的用戶、以及用戶名為 ‘joebob’ 和 ‘billyjean’ 的用戶(不一定是 administrator 角色)可見
[eyesonly logged="in"] 已登錄用戶可見 [/eyesonly]
[eyesonly logged="out"] 未登錄用戶可見 [/eyesonly]
在同一個位置顯示內(nèi)容,對已登錄和未登錄用戶顯示不同內(nèi)容
[eyesonly level="administrator"] 任何內(nèi)容 [/eyesonly]
只有 administrator 角色的用戶可見
[eyesonly hide="yes" level="administrator custom_capability"] 任何內(nèi)容 [/eyesonly]
內(nèi)容對 administrator 角色的用戶、以及擁有能力 ‘custom_capability’ 的用戶隱藏
代碼出自:https://wordpress.org/plugins/eyes-only-user-access-shortcode/ 你可以訪問該頁面查看更多介紹。





eyes only有漢化版嗎
隱藏后的提示呢? 有提示嗎? 可以自定義提示信息嗎?
求漢化版本
對部分內(nèi)容隱藏后,為什么沒有提示說對該內(nèi)容影響。登錄可見或其他?
這要是個插件就好了
這個本來就是插件 https://wordpress.org/plugins/eyes-only-user-access-shortcode/
嘿,寫出直接安裝eyesonly即可實現(xiàn)這個操作更好了,給那些不太喜歡折騰的孩紙們~