如果您想限制某个功能(内置或自定义),可以使用current_user_can() 并通过相应的capability.
if( current_user_can(\'manage_options\') ) {
echo "Hi there user who can manage options\';
}
请记住,帖子或页面内容是一块,您需要做一些更为自定义的事情来隐藏某些段落等。
在这种情况下,您可能需要一个短代码来包装要隐藏的内容-实际上,对于一个很可能已经存在的插件来说,这是一个好主意:)
编辑:
我很快就写了这个。需要一些测试。cap是可选的,如果未设置,则默认为必须登录才能查看内容。
class Hide_Content {
function render_shortcode( $atts, $content = "" ) {
//get the atts passed to the shortcode
$atts = shortcode_atts( array(
\'cap\' => false,
), $atts );
//we need to be logged in either way
if( is_user_logged_in() ) {
// there is a cap set and the user can do it - we\'re good
if( $atts[\'cap\'] && current_user_can( $atts[\'cap\']) ) {
return "content {$atts[\'cap\']} = $content";
}else{
//if there is no cap set - we\'ll just show it to logged in users
return \'Logged in only :: \' . $content ;
}
}
}
}
add_shortcode( \'hide_content\', array( \'Hide_Content\', \'render_shortcode\' ) );
//useage
[hide_content cap="manage_options"]This is hidden from non-logged in and users who can\'t manage options[/hide_content]