我允许作者用户角色添加/编辑帖子(使用称为“用户帖子”的自定义帖子类型),以及从wp admin添加/编辑自己的媒体。我目前已将其设置为他们看到的唯一菜单项是“User Posts”、“New User Post”、“Media”和“Upload Media”,URL为:Post New。php?post_type=tsv_userpostedit。php?post\\U type=tsv\\U USERPOSTPLOAD。PHP媒体新。php
唯一的问题是,如果用户键入的URL是,例如,post new。php(没有自定义帖子类型部分)Wordpress仍然允许他们查看该页面。没有bueno。
如果用户不是管理员并且登录到除上述URL以外的任何wp admin页面,是否有方法重定向或显示错误消息给用户?
我曾尝试从另一个线程使用此代码(仅针对post-new.php页面),但它似乎没有任何作用:
//Show error message if authors try to access wrong wp-admin pages
add_action(\'admin_head\',\'my_restrict_access\');
function my_restrict_access_meassage(){
global $pagenow;
if ($pagenow == \'post-new.php\' && !current_user_can( \'switch_themes\' )){
echo \'<div class="wrap"><br />
<div id="message" class="error">You Dont have the right permissions to access this page</div>
</div>\';
exit();
}
}
谢谢是预付款。
EDIT: 以下是我最终得到的代码:
//Restrict access to authors in wp-admin pages
//Show error message if authors try to access wrong wp-admin pages
add_action(\'admin_head\',\'my_restrict_access\');
function my_restrict_access(){
$Path=$_SERVER[\'REQUEST_URI\'];
$basepath=\'http://www.theseattlevine.com/wordpress/wp-admin\';
$URI=\'http://www.theseattlevine.com\'.$Path;
if ( ($URI ==($basepath . \'/post-new.php\')) && !current_user_can (\'manage_categories\') ) {
echo \'<div class="wrap"><br />
<div id="message" class="error">You Dont have the right permissions to access this page. Try these links instead:
<ul>
<li><a href="/">The Vine Homepage</a></li>
<li><a href="/wordpress/wp-admin/post-new.php?post_type=tsv_userpost">Add a new user post</a></li>
<li><a href="/dashboard">Your dashboard</a></li>
</ul>
</div>
</div>\';
exit();
}
elseif ( ($URI ==($basepath . \'/edit.php\')) && !current_user_can (\'manage_categories\') ) {
echo \'<div class="wrap"><br />
<div id="message" class="error">You Dont have the right permissions to access this page. Try these links instead:
<ul>
<li><a href="/">The Vine Homepage</a></li>
<li><a href="/wordpress/wp-admin/post-new.php?post_type=tsv_userpost">Add a new user post</a></li>
<li><a href="/dashboard">Your dashboard</a></li>
</ul>
</div>
</div>\';
exit();
}
}