完全隐藏WordPress页面



我开发了我的第一个插件,它通过这种方法以编程方式创建一些页面:

$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status'    => 'close',
'post_author'    => 1,
'post_title'     => 'HSN Form',
'post_name'      => strtolower(str_replace(' ', '-', trim('hsn-form'))),
'post_status'    => 'publish',
'post_type'      => 'page',
)
);

我为它设置了模板文件:

add_filter( 'page_template', 'hsn_service_form_page_template', 10, 1 );
function hsn_service_form_page_template( $page_template ){
if ( is_page( 'hsn-form' ) ) {
$page_template = plugin_dir_path(__DIR__) . 'service-form/form-template.php';
}
return $page_template;
}

在我想从wordpress仪表板中完全隐藏它之后,但这些必须通过以下方式可用:

wwww.example.com/hsn-form。

我可以从"页面"菜单中隐藏以下代码:

add_filter( 'parse_query', 'ts_hide_pages_in_wp_admin' );
function ts_hide_pages_in_wp_admin($query) {
global $pagenow,$post_type;
$page = get_page_by_path('hsn-form');
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__not_in'] = array($page->ID);
}
}

没关系,但它仍然在Appereance->Menu中可用,您可以在其中创建导航菜单。

我搜索了它,但我找不到我的问题的完整解决方案:

  1. 我的插件必须创建一些以这种方式可用的页面:www.example.com/hsn-form
  2. 我需要从管理仪表板中完全隐藏此页面
  3. 我想为这些页面应用模板以添加一些自定义 php 代码。

因此,如果有人应该知道一个完整的解决方案,那应该很棒。 提前谢谢你!

将parse_query更改为pre_get_posts

为了从管理员导航菜单中删除页面,您可以挂钩到admin_menu操作,然后操作全局$submenu变量。

add_action('admin_menu', function(){
global $submenu;
array_walk($submenu, function(&$child, $parent){
if( $parent != 'edit.php' )
return;
foreach($child as $key=>$submenu){
if( $submenu[2] == 'post-new.php' ) {
unset($child[$key]);
break;
}
}
});
});

在该示例中,我们正在寻找一个带有post-new.php的子页面 在带有 slugedit.php的顶级页面下方。如果找到,它将从导航菜单中完全删除。

$submenu[2]部分正在数组中查找 slug 元素。如果要改为按子菜单名称进行匹配,可以将其替换为$submenu[0]

最新更新