简体   繁体   English

WordPress在激活插件时添加菜单页面

[英]Wordpress adding a menu page when activating a plugin

Im making a custom plugin for wordpress and i need to create a page in the admin menu. 我为wordpress制作了一个自定义插件,我需要在管理菜单中创建一个页面。 I already have a file called mailing_list.php with the following code: 我已经有一个名为mailing_list.php的文件,其中包含以下代码:

function jps_mail_list_page_entry() {
add_menu_page(
    __('JPS Mailing List'),
    'JPS Mailing List',
    'manage_options',
    'jpsNews_mailinglist',
    'jpsNews_mailing_list',
    'dashicons-email'
);
}
add_action('admin_menu', 'jps_mail_list_page_entry');


function jpsNews_mailing_list() {
    echo 'hello';
}

Now, in the plugin page i have this: 现在,在插件页面中,我有这个:

function jpsNews_activate_plugin() {
    include_once(plugin_dir_path(__FILE__).'pages/mailing-list.php');
}
register_activation_hook(__FILE__,'jpsNews_activate_plugin');

Its not working so, is it even possible to do it like this? 它不起作用,甚至有可能这样做吗? how can i do it? 我该怎么做?

thanks in advance. 提前致谢。

I'm not sure if you have purposely added the function for the echo 'hello', but that's going to echo hello in the content area of the plug-in page. 我不确定您是否故意为echo'hello'添加了该函数,但这将在插件页面的内容区域中生成hello。

For your mailing-list.php 为您的mailing-list.php

function jps_mail_list_page_entry() {
   add_menu_page(
        __('JPS Mailing List', 'jpsNews_mailinglist' ),
        'JPS Mailing List',
        'manage_options',
        'jpsNews_mailinglist',
        'jpsNews_mailing_list',
        'dashicons-email'
   );
}
add_action('admin_menu', 'jps_mail_list_page_entry');

for the plug-in page, you can simply do: 对于插件页面,您可以简单地执行以下操作:

include_once(plugin_dir_path(__FILE__).'/pages/mailing-list.php');

Got it to work. 得到它的工作。 turns out the function to add the pages must be outside the other functions. 原来添加页面的功能必须在其他功能之外。 just put theme at the end of the script outside the activation hook and it worked. 只需将主题放在激活挂钩之外脚本的末尾即可,它可以正常工作。

// DISPLAY PAGES AND SUBPAGES
function jps_mail_list_page_entry() {
    add_menu_page(
        __('JPS Mailing List'),
        'JPS Mailing List',
        'manage_options',
        'jpsNews_mailinglist',
        'jpsNews_mailing_list',
       'dashicons-email'
    );
}
add_action('admin_menu', 'jps_mail_list_page_entry');


function jpsNews_mailing_list() {
    include_once(plugin_dir_path( __FILE__ ) . 'pages/mailing-list.php');
}

Anyway, Thanks =) 无论如何,谢谢=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM