WordPress: Adding Administration Menus:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
第1行: 第1行:
== Introduction ==
== 介绍==
== 介绍==
Many plugins give their users options or settings, which allow the users a way to customize how the plugin is used. As a plugin author, you have several choices in how to set this up. One way is to have the user edit information in the plugin's PHP file, but this makes many users uncomfortable (to say the least). So, a better way is usually to create administration screens that let the plugin user set options in a more familiar manner.


许多插件向用户提供选项或者设置,使得用户能够自定义插件的使用方式。作为插件作者,你可以使用多种方式设置这个功能。一种方式是使用户在插件的PHP文件中编辑信息,但多数用户觉得这样做不便利(至少是这样觉得的)。因此,更好的方法通常是创建管理界面,使得插件用户更方便地设置选项。
许多插件向用户提供选项或者设置,使得用户能够自定义插件的使用方式。作为插件作者,你可以使用多种方式设置这个功能。一种方式是使用户在插件的PHP文件中编辑信息,但多数用户觉得这样做不便利(至少是这样觉得的)。因此,更好的方法通常是创建管理界面,使得插件用户更方便地设置选项。
This article explains how to add custom administration screens to WordPress in a plugin. It assumes you are already familiar with the basics of [[WordPress:Writing a Plugin]] and the [[WordPress:Plugin API]] of Actions and Filters.


这篇文件介绍了怎样在插件中为WordPress添加自定义管理界面。前提是你已经熟悉[[WordPress:Writing a Plugin|编写插件]]和Actions 和Filters的[[WordPress:Plugin API|插件API]]的基本知识。
这篇文件介绍了怎样在插件中为WordPress添加自定义管理界面。前提是你已经熟悉[[WordPress:Writing a Plugin|编写插件]]和Actions 和Filters的[[WordPress:Plugin API|插件API]]的基本知识。
== Menus and Submenus ==


== 菜单和子菜单 ==
== 菜单和子菜单 ==
The first step in creating custom administration screens is to figure out where in the WordPress administration menu system they belong. Most plugins add their screens to the existing WordPress top-level menus. Here is a guide to what belongs in which top-level menu:


创建自定义管理界面,首先要了解界面属于WordPress管理菜单系统的哪个部分。大多数插件将界面添加到WordPress当前最高级别的菜单。下面有个指南,关于最高级别菜单中,有哪些内容:
创建自定义管理界面,首先要了解界面属于WordPress管理菜单系统的哪个部分。大多数插件将界面添加到WordPress当前最高级别的菜单。下面有个指南,关于最高级别菜单中,有哪些内容:
;[[WordPress:Administration_Panels#Settings_-_Configuration_Settings|Settings]]:Displays plugin options that only administrators should view (also see [[WordPress:Creating_Options_Pages|Creating Settings Pages]]).


;[[WordPress:Administration_Panels#Settings_-_Configuration_Settings|设置]]:显示只有管理员才能够看到的插件选项(也看看[[WordPress:Creating_Options_Pages|创建设置页面]])。
;[[WordPress:Administration_Panels#Settings_-_Configuration_Settings|设置]]:显示只有管理员才能够看到的插件选项(也看看[[WordPress:Creating_Options_Pages|创建设置页面]])。
;[[WordPress:Administration_Panels#Manage_-_Change_your_content|Manage]]:Displays management controls for links, posts, categories, images, etc.


;[[WordPress:Administration_Panels#Manage_-_Change_your_content|管理]]:显示链接,文章,类别,图像,等等的管理控制。
;[[WordPress:Administration_Panels#Manage_-_Change_your_content|管理]]:显示链接,文章,类别,图像,等等的管理控制。
;[[WordPress:Administration_Panels#Plugins_-_Add_Functionality_to_your_Blog|Plugins]]:Displays controls dealing with plugin management, not configuration options for a plugin itself.


;[[WordPress:Administration_Panels#Plugins_-_Add_Functionality_to_your_Blog|插件]]:显示插件管理,而不是插件自身配置选项的控制。
;[[WordPress:Administration_Panels#Plugins_-_Add_Functionality_to_your_Blog|插件]]:显示插件管理,而不是插件自身配置选项的控制。
;[[WordPress:Administration_Panels#Presentation_-_Change_the_Look_of_your_Blog|Presentation]]:Displays controls for manipulation of theme/style files, sidebars, etc.


;[[WordPress:Administration_Panels#Presentation_-_Change_the_Look_of_your_Blog|Presentation]]:显示操作主题/样式文件,边栏等等的控制功能。
;[[WordPress:Administration_Panels#Presentation_-_Change_the_Look_of_your_Blog|Presentation]]:显示操作主题/样式文件,边栏等等的控制功能。
;[[WordPress:Administration_Panels#Write_-_Make_some_content|Write]]:Displays tools for writing content (posts and pages).


;[[WordPress:Administration_Panels#Write_-_Make_some_content|写]]:显示编写内容(文章和网页)的工具。
;[[WordPress:Administration_Panels#Write_-_Make_some_content|写]]:显示编写内容(文章和网页)的工具。
;[[WordPress:Administration_Panels#Users_-_Your_Blogging_Family|Users]]:Displays controls for user management.


;[[WordPress:Administration_Panels#Users_-_Your_Blogging_Family|用户]]:显示控制用户管理的功能。
;[[WordPress:Administration_Panels#Users_-_Your_Blogging_Family|用户]]:显示控制用户管理的功能。
If your plugin introduces an entirely new concept or feature to WordPress, and needs many screens to do it, another option is to create a new top-level menu for your plugin. This should only be considered if you really need multiple, related screens to make WordPress do something it was not originally designed to do. Examples might include management of image galleries, database administration, or conference management.


如果你的插件介绍了全新的WordPress 理念或者功能,而且需要许多界面实现这个功能,另一个选项是为你的插件创建全新的最高级别的菜单。如果你真的需要多个相关的界面,使得WordPress执行最初设计的操作范围外的一些操作,你就需要考虑插件的这些功能。例子包括图像集管理,数据库管理,或者会议管理。
如果你的插件介绍了全新的WordPress 理念或者功能,而且需要许多界面实现这个功能,另一个选项是为你的插件创建全新的最高级别的菜单。如果你真的需要多个相关的界面,使得WordPress执行最初设计的操作范围外的一些操作,你就需要考虑插件的这些功能。例子包括图像集管理,数据库管理,或者会议管理。
With the [http://barunsingh.com/software/custom-admin-menu/ Custom Admin Menu plugin], users now also have the ability to decide for themselves if they want your plugin to occupy a top-level menu item or not -- you may want to make clear to users that they have that option, particularly if you've decided to create a top-level menu item.


使用[http://barunsingh.com/software/custom-admin-menu/ 自定义管理菜单插件],用户现在也能够决定是否使自己的插件复制最高级别的菜单内容—你可能想向用户阐明他们可以那样做,特别是如果你已经创建了自定义的最高级别的菜单内容。
使用[http://barunsingh.com/software/custom-admin-menu/ 自定义管理菜单插件],用户现在也能够决定是否使自己的插件复制最高级别的菜单内容—你可能想向用户阐明他们可以那样做,特别是如果你已经创建了自定义的最高级别的菜单内容。
== Admin Menu Functions ==


== 管理菜单函数 ==
== 管理菜单函数 ==
Now that you have decided where to add your menu/submenu, the next step is to tell WordPress about your new pages. All of this will take place in a function you have registered as an <tt>'admin_menu'</tt> action (see example at bottom of this section).


既然你已经决定在哪里添加菜单/子菜单,接着就是要向WordPress展示你的新页面。所有这些操作,都会在注册为<tt>'admin_menu'</tt> action的函数中发生(请看看这个部分的底部的例子)。
既然你已经决定在哪里添加菜单/子菜单,接着就是要向WordPress展示你的新页面。所有这些操作,都会在注册为<tt>'admin_menu'</tt> action的函数中发生(请看看这个部分的底部的例子)。
=== Top-Level menu ===


=== 最高级别菜单===
=== 最高级别菜单===
The first thing you'll need to do is to create a top-level menu, if you have decided that your plugin really needs one. You'll use the <tt>add_menu_page</tt> function:


首先,如果你已经确定插件需要最高级别菜单,你要创建一个。你将会使用<tt>add_menu_page</tt>函数:
首先,如果你已经确定插件需要最高级别菜单,你要创建一个。你将会使用<tt>add_menu_page</tt>函数:
第69行: 第35行:
add_menu_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
add_menu_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');


add_menu_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
Parameter values:
参数值:
参数值:
;page_title:Text that will go into the HTML page title for the page when the menu is active.


;page_title:菜单激活的时候,文本会进入HTML网页标题。
;page_title:菜单激活的时候,文本会进入HTML网页标题。
;menu_title:The on-screen name text for the menu.


;menu_title:菜单的界面名文本。
;menu_title:菜单的界面名文本。
;access_level/capability:The minimum [[WordPress:User Levels|user level]] or the [[WordPress:Roles_and_Capabilities|capability]] required to display and use this menu page.


;access_level/capability:用来显示和使用这个菜单页面的最小的[[WordPress:User Levels|用户级别]] 或者[[WordPress:Roles_and_Capabilities|权限]]。
;access_level/capability:用来显示和使用这个菜单页面的最小的[[WordPress:User Levels|用户级别]] 或者[[WordPress:Roles_and_Capabilities|权限]]。


;file:The PHP file that handles the display of the menu page content.
;文件:控制菜单页面内容的显示情况的PHP文件。
;function: The function that displays the page content for the menu page.
 
;文件:控制菜单页面内容的显示情况的PHP文件。
;函数:显示菜单页面内容的函数。
;函数:显示菜单页面内容的函数。
Technically, the ''function'' parameter is optional, but if it is not supplied, then WordPress will basically assume that including the PHP file will generate the administration screen, without calling a function. Most plugin authors choose to put the page-generating code in a function within their main plugin file.


从技术上说,''function''参数是可选择的,但是如果没有应用这个参数,WordPress会认定包含了PHP文件,不需要调用函数,就会创建管理界面。大多数插件作者选择将创建网页的代码,放入主要的插件文件的函数中。
从技术上说,''function''参数是可选择的,但是如果没有应用这个参数,WordPress会认定包含了PHP文件,不需要调用函数,就会创建管理界面。大多数插件作者选择将创建网页的代码,放入主要的插件文件的函数中。


=== Sub-Menus ===
=== 子菜单 ===
=== 子菜单 ===
Once you have a top-level menu defined, or have chosen to use an existing WordPress top-level menu, you are ready to define one or more sub-menu pages using the <tt>add_submenu_page</tt> function. Make sure to add the submenu pages in the order you want them displayed.


你定义了最高级别的菜单,或者已经选择使用WordPress当前最高级别的菜单,你就要准备使用<tt>add_submenu_page</tt>函数,定义一个或者更多的子菜单页面。确定添加子菜单页面的顺序,与页面的显示顺序相同。
你定义了最高级别的菜单,或者已经选择使用WordPress当前最高级别的菜单,你就要准备使用<tt>add_submenu_page</tt>函数,定义一个或者更多的子菜单页面。确定添加子菜单页面的顺序,与页面的显示顺序相同。
第105行: 第54行:
add_submenu_page(''parent'', ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
add_submenu_page(''parent'', ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');


add_submenu_page(''parent'', ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
Parameter values:
参数值:
参数值:
;parent:The filename of the core WordPress admin file that supplies the top-level menu in which you want to insert your submenu, or your plugin file if this submenu is going into a custom top-level menu.  (Examples: index.php, post.php, edit.php, themes.php, plugins.php, etc.)
;parent:WordPress核心管理文件的文件名,这个文件应用了最高级别的菜单,你希望将子菜单插入到这个菜单中,如果子菜单进入自定义最高级别的菜单,你可以插入插件(例子:index.php, post.php, edit.php, themes.php, plugins.php,等等)
;parent:WordPress核心管理文件的文件名,这个文件应用了最高级别的菜单,你希望将子菜单插入到这个菜单中,如果子菜单进入自定义最高级别的菜单,你可以插入插件(例子:index.php, post.php, edit.php, themes.php, plugins.php,等等)
;page_title:Text that will go into the HTML page title for the page when the submenu is active.
;page_title:激活子菜单时,进入网页HTML网页标题的文本。
;page_title:激活子菜单时,进入网页HTML网页标题的文本。
;menu_title:The on-screen name text for the submenu.
;menu_title:子菜单的界面名文本。
;menu_title:子菜单的界面名文本。
;access_level/capability:The minimum [[WordPress:User Levels|user level]] or the [[WordPress:Roles_and_Capabilities|capability]] required to display and use this submenu page.
;access_level/权限:显示并且使用这个子菜单页面的最小的[[WordPress:User Levels|用户级别]] 或者[[WordPress:Roles_and_Capabilities|权限]]。
;access_level/权限:显示并且使用这个子菜单页面的最小的[[WordPress:User Levels|用户级别]] 或者[[WordPress:Roles_and_Capabilities|权限]]。
;file:For existing WordPress menus, the PHP file that handles the display of the menu page content. For submenus of a custom top-level menu, a '''unique''' identifier for this sub-menu page.
;file:对于当前存在的WordPress菜单,指的是控制菜单内容显示情况的PHP文件。自定义最高级别菜单的子菜单页面有'''唯一的'''标示符。
;file:对于当前存在的WordPress菜单,指的是控制菜单内容显示情况的PHP文件。自定义最高级别菜单的子菜单页面有'''唯一的'''标示符。
''In situations where a plugin is creating it's own top-level menu, the first submenu will normally have the same link title as the top-level menu and hence the link will be duplicated. The duplicate link title can be avoided by calling the add_submenu_page function with the parent and file parameters being given the same value.
''


''插件创建自身最高级别菜单的时候,第一个子菜单的链接标题通常与最高级别菜单的链接标题是相同的,因此链接得到了复制。使用相同参数值的母和文件参数,调用add_submenu_page函数,可以避免复制链接标题。
''插件创建自身最高级别菜单的时候,第一个子菜单的链接标题通常与最高级别菜单的链接标题是相同的,因此链接得到了复制。使用相同参数值的母和文件参数,调用add_submenu_page函数,可以避免复制链接标题。
;function: The function that displays the page content for the menu page.


;function:显示菜单页面的网页内容的函数。
;function:显示菜单页面的网页内容的函数。
Technically, as in the <tt>add_menu_page</tt> function, the ''function'' parameter is optional, but if it is not supplied, then WordPress will basically assume that including the PHP file will generate the administration screen, without calling a function. Most plugin authors choose to put the page-generating code in a function within their main plugin file.


一般来说,就如<tt>add_menu_page</tt>函数,''function''参数是可选的,但是如果没有提供这个函数,WordPress会推测不需要调用函数,包含PHP文件,就会产生管理界面。大多数插件作者选择将产生网页的代码放入主要的插件文件的函数中。
一般来说,就如<tt>add_menu_page</tt>函数,''function''参数是可选的,但是如果没有提供这个函数,WordPress会推测不需要调用函数,包含PHP文件,就会产生管理界面。大多数插件作者选择将产生网页的代码放入主要的插件文件的函数中。
Here's a quick example, illustrating how to insert a top-level menu page and a sub-menu page, where the title on the sub-menu page is different from the top-level page. In this example, 'my_magic_function' is the name of the function that displays the first sub-menu page:


下面有个例子,解释了怎样插入顶级菜单页面和子菜单页面,子菜单页面的标题和顶级页面的标题有什么不同。在这个例子中,'my_magic_function'是显示第一个子菜单页面的函数名。
下面有个例子,解释了怎样插入顶级菜单页面和子菜单页面,子菜单页面的标题和顶级页面的标题有什么不同。在这个例子中,'my_magic_function'是显示第一个子菜单页面的函数名。
第151行: 第73行:
add_submenu_page(__FILE__, 'Page title', 'Sub-menu title', 8, __FILE__, 'my_magic_function');
add_submenu_page(__FILE__, 'Page title', 'Sub-menu title', 8, __FILE__, 'my_magic_function');
</pre>
</pre>
<pre>
add_menu_page('Page title', 'Top-level menu title', 8, __FILE__, 'my_magic_function');
add_submenu_page(__FILE__, 'Page title', 'Sub-menu title', 8, __FILE__, 'my_magic_function');
</pre>
Since most submenus go into WordPress's Options, Management, or Presentation menus, WordPress supplies three wrapper functions that make adding a submenu to those pages easier:


因为大多数子菜单页面在WordPress选项,管理,或者Presentation菜单中,WordPress提供了三个包装函数,向这些页面添加子菜单变得更加简单:
因为大多数子菜单页面在WordPress选项,管理,或者Presentation菜单中,WordPress提供了三个包装函数,向这些页面添加子菜单变得更加简单:


;For the Options top-level menu (see [[WordPress:Creating Options Pages]] for more on this):
;关于选项顶级菜单(更多的内容,请看看[[WordPress:Creating Options Pages|创建选项页面]]):
 
;关于选项顶级菜单(更多的内容,请看看[[WordPress:Creating Options Pages|创建选项页面]]):


  add_options_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
  add_options_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
第170行: 第83行:
;For Presentation:
;For Presentation:
  add_theme_page( ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
  add_theme_page( ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
add_options_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
;关于管理:
add_management_page(''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
;For Presentation:
add_theme_page( ''page_title'', ''menu_title'', ''access_level/capability'', ''file'', ''[function]'');
=== Inserting the Pages ===


=== 插入页面 ===
=== 插入页面 ===
Here is an example of a WordPress plugin that inserts new menus into various places:


下面是WordPress插件的例子,在不同的位置,插入新的菜单:
下面是WordPress插件的例子,在不同的位置,插入新的菜单:
<pre>
<?php
/*
Plugin Name: Menu Test
Plugin URI: http://wordpress.org
Description: Menu Test
Author: Nobody
Author URI: http://example.com
*/
// Hook for adding admin menus
add_action('admin_menu', 'mt_add_pages');
// action function for above hook
function mt_add_pages() {
    // Add a new submenu under Options:
    add_options_page('Test Options', 'Test Options', 8, 'testoptions', 'mt_options_page');
    // Add a new submenu under Manage:
    add_management_page('Test Manage', 'Test Manage', 8, 'testmanage', 'mt_manage_page');
    // Add a new top-level menu (ill-advised):
    add_menu_page('Test Toplevel', 'Test Toplevel', 8, __FILE__, 'mt_toplevel_page');
    // Add a submenu to the custom top-level menu:
    add_submenu_page(__FILE__, 'Test Sublevel', 'Test Sublevel', 8, 'sub-page', 'mt_sublevel_page');
    // Add a second submenu to the custom top-level menu:
    add_submenu_page(__FILE__, 'Test Sublevel 2', 'Test Sublevel 2', 8, 'sub-page2', 'mt_sublevel_page2');
}
// mt_options_page() displays the page content for the Test Options submenu
function mt_options_page() {
    echo "<h2>Test Options</h2>";
}
// mt_manage_page() displays the page content for the Test Manage submenu
function mt_manage_page() {
    echo "<h2>Test Manage</h2>";
}
// mt_toplevel_page() displays the page content for the custom Test Toplevel menu
function mt_toplevel_page() {
    echo "<h2>Test Toplevel</h2>";
}
// mt_sublevel_page() displays the page content for the first submenu
// of the custom Test Toplevel menu
function mt_sublevel_page() {
    echo "<h2>Test Sublevel</h2>";
}
// mt_sublevel_page2() displays the page content for the second submenu
// of the custom Test Toplevel menu
function mt_sublevel_page2() {
    echo "<h2>Test Sublevel 2</h2>";
}
?>
</pre>
<pre>
<pre>
<?php
<?php
第319行: 第147行:
?>
?>
</pre>
</pre>
=== Sample Menu Page ===


=== 样本菜单页面 ===
=== 样本菜单页面 ===
The example above contains several dummy functions, such as <tt>mt_options_page</tt>, as placeholders for actual page content. We need to turn them into real menu pages. So, let's assume that our plugin has an option called mt_favorite_food, and that we want to allow the site owner to type in his/her favorite food on the plugin's Option page. The <tt>mt_options_page</tt> function will need to put a data entry form on the screen to enable this, and also process the entered data. Here is a function that does this:


上述的例子包含了几个哑元函数,如<tt>mt_options_page</tt>,作为真正的网页内容的占位符。我们需要将这些函数转变为真正的菜单页面。同时,加入我们的插件有个称为mt_favorite_food的选项,而且我们希望站点作者在插件的选项页面上输入他/她最喜爱的食物。<tt>mt_options_page</tt>函数需要将数据entry 形式放到界面上,实现这个操作,同时处理输入的数据。下面有个函数,可以执行这个操作:
上述的例子包含了几个哑元函数,如<tt>mt_options_page</tt>,作为真正的网页内容的占位符。我们需要将这些函数转变为真正的菜单页面。同时,加入我们的插件有个称为mt_favorite_food的选项,而且我们希望站点作者在插件的选项页面上输入他/她最喜爱的食物。<tt>mt_options_page</tt>函数需要将数据entry 形式放到界面上,实现这个操作,同时处理输入的数据。下面有个函数,可以执行这个操作:
<pre>
// mt_options_page() displays the page content for the Test Options submenu
function mt_options_page() {
    // variables for the field and option names
    $opt_name = 'mt_favorite_food';
    $hidden_field_name = 'mt_submit_hidden';
    $data_field_name = 'mt_favorite_food';
    // Read in existing option value from database
    $opt_val = get_option( $opt_name );
    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if( $_POST[ $hidden_field_name ] == 'Y' ) {
        // Read their posted value
        $opt_val = $_POST[ $data_field_name ];
        // Save the posted value in the database
        update_option( $opt_name, $opt_val );
        // Put an options updated message on the screen
?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php
    }
    // Now display the options editing screen
    echo '<div class="wrap">';
    // header
    echo "<h2>" . __( 'Menu Test Plugin Options', 'mt_trans_domain' ) . "</h2>";
    // options form
   
    ?>
<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<p><?php _e("Favorite Color:", 'mt_trans_domain' ); ?>
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
</p><hr />
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'mt_trans_domain' ) ?>" />
</p>
</form>
</div>
<?php
}
</pre>


<pre>
<pre>
第473行: 第215行:


几点注意:
几点注意:
* The WordPress administration functions take care of validating the user, so you don't have to worry about it in your function.


*WordPress管理函数能够确认用户,因此你的函数不需要确认。
*WordPress管理函数能够确认用户,因此你的函数不需要确认。
* The function example above has been internationalized -- see [[WordPress:Writing_a_Plugin#Internationalizing_Your_Plugin|the Internationalization section of Writing a Plugin]] for more information


*上述的函数例子已经国际化了—更多的信息,请看看[[WordPress:Writing_a_Plugin#Internationalizing_Your_Plugin|编写插件的国际化部分]]
*上述的函数例子已经国际化了—更多的信息,请看看[[WordPress:Writing_a_Plugin#Internationalizing_Your_Plugin|编写插件的国际化部分]]
* The function processes any entered data before putting the data entry form on the screen, so that the new values will be shown in the form (rather than the values from the database).


*函数处理好了输入的数据后,才将数据库entry form放到界面上,因此形式会显示新的参数值(而不是数据库参数值)。
*函数处理好了输入的数据后,才将数据库entry form放到界面上,因此形式会显示新的参数值(而不是数据库参数值)。
* You don't have to worry about this working the first time, because the WordPress <tt>update_option</tt> function will automatically add an option to the database if it doesn't already exist.


*第一次,你不需要担心选项的运行,因为如果选项不存在,WordPress<tt>update_option</tt>会自动将选项添加到数据库中。
*第一次,你不需要担心选项的运行,因为如果选项不存在,WordPress<tt>update_option</tt>会自动将选项添加到数据库中。

2008年8月23日 (六) 11:53的版本

介绍

许多插件向用户提供选项或者设置,使得用户能够自定义插件的使用方式。作为插件作者,你可以使用多种方式设置这个功能。一种方式是使用户在插件的PHP文件中编辑信息,但多数用户觉得这样做不便利(至少是这样觉得的)。因此,更好的方法通常是创建管理界面,使得插件用户更方便地设置选项。

这篇文件介绍了怎样在插件中为WordPress添加自定义管理界面。前提是你已经熟悉编写插件和Actions 和Filters的插件API的基本知识。

菜单和子菜单

创建自定义管理界面,首先要了解界面属于WordPress管理菜单系统的哪个部分。大多数插件将界面添加到WordPress当前最高级别的菜单。下面有个指南,关于最高级别菜单中,有哪些内容:

设置
显示只有管理员才能够看到的插件选项(也看看创建设置页面)。
管理
显示链接,文章,类别,图像,等等的管理控制。
插件
显示插件管理,而不是插件自身配置选项的控制。
Presentation
显示操作主题/样式文件,边栏等等的控制功能。
显示编写内容(文章和网页)的工具。
用户
显示控制用户管理的功能。

如果你的插件介绍了全新的WordPress 理念或者功能,而且需要许多界面实现这个功能,另一个选项是为你的插件创建全新的最高级别的菜单。如果你真的需要多个相关的界面,使得WordPress执行最初设计的操作范围外的一些操作,你就需要考虑插件的这些功能。例子包括图像集管理,数据库管理,或者会议管理。

使用自定义管理菜单插件,用户现在也能够决定是否使自己的插件复制最高级别的菜单内容—你可能想向用户阐明他们可以那样做,特别是如果你已经创建了自定义的最高级别的菜单内容。

管理菜单函数

既然你已经决定在哪里添加菜单/子菜单,接着就是要向WordPress展示你的新页面。所有这些操作,都会在注册为'admin_menu' action的函数中发生(请看看这个部分的底部的例子)。

最高级别菜单

首先,如果你已经确定插件需要最高级别菜单,你要创建一个。你将会使用add_menu_page函数:

add_menu_page(page_title, menu_title, access_level/capability, file, [function]);

参数值:

page_title
菜单激活的时候,文本会进入HTML网页标题。
menu_title
菜单的界面名文本。
access_level/capability
用来显示和使用这个菜单页面的最小的用户级别 或者权限
文件:控制菜单页面内容的显示情况的PHP文件。
函数
显示菜单页面内容的函数。

从技术上说,function参数是可选择的,但是如果没有应用这个参数,WordPress会认定包含了PHP文件,不需要调用函数,就会创建管理界面。大多数插件作者选择将创建网页的代码,放入主要的插件文件的函数中。

子菜单

你定义了最高级别的菜单,或者已经选择使用WordPress当前最高级别的菜单,你就要准备使用add_submenu_page函数,定义一个或者更多的子菜单页面。确定添加子菜单页面的顺序,与页面的显示顺序相同。

add_submenu_page(parent, page_title, menu_title, access_level/capability, file, [function]);

参数值:

parent
WordPress核心管理文件的文件名,这个文件应用了最高级别的菜单,你希望将子菜单插入到这个菜单中,如果子菜单进入自定义最高级别的菜单,你可以插入插件(例子:index.php, post.php, edit.php, themes.php, plugins.php,等等)
page_title
激活子菜单时,进入网页HTML网页标题的文本。
menu_title
子菜单的界面名文本。
access_level/权限
显示并且使用这个子菜单页面的最小的用户级别 或者权限
file
对于当前存在的WordPress菜单,指的是控制菜单内容显示情况的PHP文件。自定义最高级别菜单的子菜单页面有唯一的标示符。

插件创建自身最高级别菜单的时候,第一个子菜单的链接标题通常与最高级别菜单的链接标题是相同的,因此链接得到了复制。使用相同参数值的母和文件参数,调用add_submenu_page函数,可以避免复制链接标题。

function
显示菜单页面的网页内容的函数。

一般来说,就如add_menu_page函数,function参数是可选的,但是如果没有提供这个函数,WordPress会推测不需要调用函数,包含PHP文件,就会产生管理界面。大多数插件作者选择将产生网页的代码放入主要的插件文件的函数中。

下面有个例子,解释了怎样插入顶级菜单页面和子菜单页面,子菜单页面的标题和顶级页面的标题有什么不同。在这个例子中,'my_magic_function'是显示第一个子菜单页面的函数名。

add_menu_page('Page title', 'Top-level menu title', 8, __FILE__, 'my_magic_function');
add_submenu_page(__FILE__, 'Page title', 'Sub-menu title', 8, __FILE__, 'my_magic_function');

因为大多数子菜单页面在WordPress选项,管理,或者Presentation菜单中,WordPress提供了三个包装函数,向这些页面添加子菜单变得更加简单:

关于选项顶级菜单(更多的内容,请看看创建选项页面):
add_options_page(page_title, menu_title, access_level/capability, file, [function]);
For Management
add_management_page(page_title, menu_title, access_level/capability, file, [function]);
For Presentation
add_theme_page( page_title, menu_title, access_level/capability, file, [function]);

插入页面

下面是WordPress插件的例子,在不同的位置,插入新的菜单:

<?php
/*
插件名: 菜单测试
插件 URI: http://wordpress.org
描述: 菜单测试
作者: 没有人
作者URI: http://example.com
*/

// 添加管理菜单Hook 
add_action('admin_menu', 'mt_add_pages');

// action function for above hook
function mt_add_pages() {
    // 在选项下,添加新的子菜单:
    add_options_page('Test Options', 'Test Options', 8, 'testoptions', 'mt_options_page');

    // 在管理下,添加新的子菜单:
    add_management_page('Test Manage', 'Test Manage', 8, 'testmanage', 'mt_manage_page');

    // 添加新的顶级菜单(ill-advised):
    add_menu_page('Test Toplevel', 'Test Toplevel', 8, __FILE__, 'mt_toplevel_page');

    // 为自定义顶级菜单添加子菜单:
    add_submenu_page(__FILE__, 'Test Sublevel', 'Test Sublevel', 8, 'sub-page', 'mt_sublevel_page');

    // 为自定义顶级菜单,添加第二个子菜单:
    add_submenu_page(__FILE__, 'Test Sublevel 2', 'Test Sublevel 2', 8, 'sub-page2', 'mt_sublevel_page2');
}

// mt_options_page() 为测试选项子菜单,显示页面内容
function mt_options_page() {
    echo "<h2>Test Options</h2>";
}

// mt_manage_page() 为测试管理子菜单,显示页面内容
function mt_manage_page() {
    echo "<h2>Test Manage</h2>";
}

// mt_toplevel_page() 显示自定义测试顶级菜单的页面内容
function mt_toplevel_page() {
    echo "<h2>Test Toplevel</h2>";
}

// mt_sublevel_page() 显示第一个菜单的页面内容
// 自定义测试顶级菜单
function mt_sublevel_page() {
    echo "<h2>Test Sublevel</h2>";
}

// mt_sublevel_page2() d显示第二个子菜单的页面内容
// 自定义测试顶级菜单
function mt_sublevel_page2() {
    echo "<h2>Test Sublevel 2</h2>";
}

?>

样本菜单页面

上述的例子包含了几个哑元函数,如mt_options_page,作为真正的网页内容的占位符。我们需要将这些函数转变为真正的菜单页面。同时,加入我们的插件有个称为mt_favorite_food的选项,而且我们希望站点作者在插件的选项页面上输入他/她最喜爱的食物。mt_options_page函数需要将数据entry 形式放到界面上,实现这个操作,同时处理输入的数据。下面有个函数,可以执行这个操作:


// mt_options_page()显示测试选项子菜单的页面内容
function mt_options_page() {

// fields和选项名称的变数
    $opt_name = 'mt_favorite_food';
    $hidden_field_name = 'mt_submit_hidden';
    $data_field_name = 'mt_favorite_food';

//阅读数据库中当前选项的参数值
    $opt_val = get_option( $opt_name );

// 看看用户有没有向我们发送一些信息
    // 如果发送了,这个隐藏的栏,就会被设置为'Y'
    if( $_POST[ $hidden_field_name ] == 'Y' ) {
        // 阅读 posted value
        $opt_val = $_POST[ $data_field_name ];

        // 将posted value 保存到数据库
        update_option( $opt_name, $opt_val );

        // 将一条选项更新信息放到界面上
?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php

    }

    // 现在显示选项编辑界面

    echo '<div class="wrap">';

    // 标头

    echo "<h2>" . __( 'Menu Test Plugin Options', 'mt_trans_domain' ) . "</h2>";

    // 选项形式
    
    ?>

<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">

<p><?php _e("Favorite Color:", 'mt_trans_domain' ); ?> 
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
</p><hr />

<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Update Options', 'mt_trans_domain' ) ?>" />
</p>

</form>
</div>

<?php
 
}

A few notes:

几点注意:

  • WordPress管理函数能够确认用户,因此你的函数不需要确认。
  • 函数处理好了输入的数据后,才将数据库entry form放到界面上,因此形式会显示新的参数值(而不是数据库参数值)。
  • 第一次,你不需要担心选项的运行,因为如果选项不存在,WordPressupdate_option会自动将选项添加到数据库中。