Wordpress add_menu_page 说找不到文件

问题描述 投票:0回答:1

我正在制作一个自定义插件。

对于我正在使用的插件文件

require_once plugin_dir_path(__FILE__) . 'includes/functions.php';

functions.php 是

add_action( 'admin_menu', 'my_Add_My_Admin_Link' );
// Add a new top level menu link to the ACP
function my_Add_My_Admin_Link()
{
  add_menu_page(
    'My Page', // Title of the page
    'Hello', // Text to show on the menu link
    'manage_options', // Capability requirement to see the link
    'includes/my-first-page.php' // The 'slug' - file to display when clicking the link
);
}

还有 my-first-page.php,它位于包含函数的目录中。 php

<div class="wrap">
<h1>Hello World</h1>
<p>Welcome</p>
</div>

我可以在管理菜单中看到该插件,但是当我单击它时,我收到一条“找不到文件”消息。

有什么想法吗?

php wordpress custom-wordpress-pages
1个回答
0
投票

这是因为您没有向 add_menu_page() 函数添加回调参数。您需要添加回调函数,其中需要包含includes/my-first-page.php 路径,这将解决问题。我在下面添加了一个示例以使其更加清楚。第四个参数是页面slug,而不是您添加的路径

/*==Register a custom menu page==*/

  function wpdocs_register_my_custom_menu_page(){
    add_menu_page( 
        __( 'Custom Menu Title', 'textdomain' ),
        'custom menu',
        'manage_options',
        'custompage',
        'my_custom_menu_page'
    ); 
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );

/*==Display a custom menu page==*/

function my_custom_menu_page(){
    include get_template_directory().'/includes/my-first-page.php';
}
© www.soinside.com 2019 - 2024. All rights reserved.