编辑:我刚刚意识到你在 edit.php 中,意思是 WordPress 的管理区域......在那种情况下,正如你提到的 `admin_init' 操作https://codex.wordpress.org/Plugin_API/Action_Reference/admin_init
尝试类似:
** 快速的事情:你说如果isset($_GET['post']) 正确,你想重定向.. 在你的例子中,你说!isset($_GET['post']) 所以必须验证你的意思。
function restrict_admin_with_redirect() {
if (isset($_GET['post'])) {
wp_redirect( site_url() );
exit;
}
}
add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );
/////////////////////////////////////// //////////////////////////
///////////////////////////////////////// /////////////////////////p>
其他信息:
如果正在执行的页面不在管理区,而是在WordPress的前端,就像普通页面或帖子一样:
有几个钩子可以用来做这个。您也可以选择编辑主题的 header.php 文件。
查看the_content 过滤器:https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
您可以将其添加到您的 functions.php 文件中(最好使用子主题,这样在更新主主题时更改不会被覆盖。)
function my_the_content_filter($content) {
if (isset($_GET['post'])){
return 'You do not have access to this page.';
}
// otherwise returns the database content
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );
或者在 header.php 文件的最开始处(在输出任何 html 之前),添加如下内容:
if (isset($_GET['post'])){
wp_redirect( {url to redirect to} );
exit();
}