热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用WordPress设置API构建自定义管理页面

Inthisguide,we’llintroducetheWordPressSettingsAPI,andcreateaWordPres

In this guide, we’ll introduce the WordPress Settings API, and create a WordPress administration page where we demonstrate the use of this API.

在本指南中,我们将介绍WordPress设置API,并创建一个WordPress管理页面,在此演示该API的用法。

For the purposes of this tutorial, we’ll wrap this functionality into a plugin, but this can also be a part of a WordPress theme.

就本教程而言,我们将把此功能包装到一个插件中,但这也可以是WordPress主题的一部分。

As the WordPress Codex says, the Settings API was added in WordPress 2.7 to streamline adding different settings fields and sections in administration pages.

正如WordPress Codex所说,Settings API是在WordPress 2.7中添加的,以简化在管理页面中添加不同的设置字段和部分的过程。

创建插件 (Creating the Plugin)

To start, we’ll create and activate a plugin to encapsulate our options page. We’ll use WP CLI to simplify the creation, although this leaves us with way more files than this guide needs.

首先,我们将创建并激活一个插件来封装我们的选项页面。 我们将使用WP CLI简化创建过程,尽管这给我们留下了比本指南需要更多的文件。

As we can see, we use wp scaffold plugin pluginname to create the plugin. Once it’s created, we activate it — optionally also using WP CLI, with wp plugin activate pluginname.

如我们所见,我们使用wp scaffold plugin pluginname来创建插件。 创建完成后,我们将其激活-可选地还可以使用WP CLI,并使用wp plugin activate pluginname

Once it’s activated, we open the main plugin file — in this case sitepoint-settings-api.php.

激活后,我们将打开主插件文件-在本例中为sitepoint-settings-api.php

创建管理页面 (Creating the Admin Page)

It isn’t necessary to use WP CLI for this plugin. We could have simply created a directory with the name of the plugin, and the PHP file inside it with the same name. Anyhow, the creation of the plugin has left us with a sitepoint-settings-api.php which looks like this:

无需为此插件使用WP CLI。 我们可以简单地创建一个带有插件名称的目录,并在其中创建具有相同名称PHP文件。 无论如何,插件的创建给我们留下了一个sitepoint-settings-api.php ,如下所示:

/**
* Plugin Name: Sitepoint Settings Api
* Plugin URI: PLUGIN SITE HERE
* Description: PLUGIN DESCRIPTION HERE
* Author: YOUR NAME HERE
* Author URI: YOUR SITE HERE
* Text Domain: sitepoint-settings-api
* Domain Path: /languages
* Version: 0.1.0
*
* @package Sitepoint_Settings_Api
*/
~

Now we can simply add code after the comment end.

现在,我们只需在注释结束后添加代码即可。

To add our options page, we’ll use add_options_page() (more details about it here). This function takes arguments as follows:

要添加我们的选项页面,我们将使用add_options_page() ( 在此处了解更多信息)。 该函数采用以下参数:

add_options_page( $page_title, $menu_title, $capability,
$menu_slug, $function );

All the arguments are self-explanatory. $menu_slug must be a unique string that WordPress will use internally, but will also be reflected in the URL. $function is a string with a name of the function that will provide HTML output for our admin page.

所有的论点都是不言而喻的。 $menu_slug必须是WordPress将在内部使用的唯一字符串,但也将反映在URL中。 $function是一个带有函数名称的字符串,该函数将为我们的管理页面提供HTML输出。

We will, therefore, add the following code to our plugin file:

因此,我们将以下代码添加到我们的插件文件中:

add_action( 'admin_menu', 'sitepoint_settings_page' );
function sitepoint_settings_page() {
add_options_page( 'Settings API Page', 'Settings API Page', 'manage_options', 'settings-api-page', 'settings_api_page' );
}

After we’ve saved the file (presuming we activated our plugin), we’ll open our administration dashboard, and we’ll find our Settings API Page under Settings in the left side menu.

保存文件后(假设我们激活了插件),我们将打开管理面板,然后在左侧菜单中的“设置”下找到“设置API页面

We can control, to a degree, the order or position of the submenu item by adding a priority argument to our add_action() function:

通过在add_action()函数中添加优先级参数,我们可以在一定程度上控制子菜单项的顺序或位置:

add_action( 'admin_menu', 'sitepoint_settings_page', 1 );

If we want to have our menu item to be in the root menu — rather than the Settings submenu — we’ll use add_menu_page(), which takes similar arguments.

如果我们希望菜单项位于根菜单中,而不是“ 设置”子菜单中,那么我们将使用add_menu_page() ,它采用类似的参数 。

Now, if we open the page in our browser, all we’ll see is an empty page, because we still haven’t created the settings_api_page() function that we specified:

现在,如果我们在浏览器中打开页面,我们将看到的只是一个空白页面,因为我们还没有创建我们指定的settings_api_page()函数:

Currently our admin page is empty

设置API (The Settings API)

The WordPress Settings API is an intricate mechanism that attempts to provide an easy way for developers to create settings pages.

WordPress设置API是一种复杂的机制,试图为开发人员提供一种轻松的方法来创建设置页面。

Before we go into a full-fledged example of the settings page displaying and saving a setting to the WordPress database, we’ll explain couple of crucial functions that WordPress provides as part of its Settings API.

在进入显示设置页面并将其保存到WordPress数据库的完整示例之前,我们将介绍WordPress作为其Settings API的一部分提供的几个关键功能。

register_setting() is a function we use to register a setting, which equals a row in wp_options table. Before we can create actual field (or fields, as setting can be an array of values), we need to register it. This way we’ll leverage the WordPress CRUD mechanism for settings. Function arguments are as follows:

register_setting()是我们用来注册设置的函数,该设置等于wp_options表中的一行。 在创建实际字段(或字段,因为设置可以是值的数组)之前,我们需要对其进行注册。 这样,我们将利用WordPress CRUD机制进行设置。 函数参数如下:

register_setting( string $option_group, string $option_name, array $args = array() )

The first two arguments are mandatory, the first one allowing us to assign fields to it, and $option_name, as we’ll see, is the actual option name in the WordPress database.

前两个参数是强制性的,第一个参数允许我们为其分配字段,而$option_name是WordPress数据库中的实际选项名称,我们将看到。

add_settings_section() defines/adds a section to an admin page. Its arguments are as follows:

add_settings_section()定义/将部分添加到管理页面。 其参数如下:

add_settings_section( string $id, string $title, callable $callback, string $page )

$callback is a function that outputs an HTL header of the section (it can be empty), and $page is the slug of the admin page we’ll display it on.

$callback是一个输出该部分的HTL标头的函数(它可以为空),而$page是我们将在其上显示的管理页面的一部分。

add_settings_field() defines a settings field within a settings section in an admin options page. Arguments for it are:

add_settings_field()在管理选项页面的设置部分中定义一个设置字段。 它的参数是:

add_settings_field( string $id, string $title, callable $callback, string $page, string $section = 'default', array $args = array()

Of these, $id, $title, $callback and $page are required. The $callback function should output the HTML of the input field.

其中$id$title$callback$page是必需的。 $callback函数应输出输入字段HTML。

The Settings API provides $page argument for add_settings_section and add_settings_field as a means to add sections and fields to existing settings pages. We’ll use stpPlugin for both our option group — in register_setting() — and for attaching the settings section and settings fields to a ‘stpPlugin’ page in the add_settings_section() and add_settings_field() functions. We’ll then “quote it” in the next two functions in our example, to output relevant HTML.

设置API为add_settings_sectionadd_settings_field提供$page参数,作为将部分和字段添加到现有设置页面的一种方法。 我们将stpPlugin用于我们的选项组(在register_setting() ,以及将设置部分和设置字段附加到add_settings_section()add_settings_field()函数中的“ stpPlugin”页面上。 然后,在示例中的下两个函数中将其“引用”,以输出相关HTML。

settings_fields() outputs “nonce, action, and option_page fields for a settings page”. It takes the $option_group argument, used in register_setting().

settings_fields()输出“设置页面的nonce,action和option_page字段”。 它采用$option_group register_setting()使用的$option_group参数。

do_settings_sections() outputs all the sections, with their respective fields, registered for a specific $page.

do_settings_sections()输出为特定$ page注册的所有节及其各自的字段。

$page is the only argument here.

$page是这里唯一的参数。

Having explained these functions, we now proceed to some actual code. The previous PHP code we added to the sitepoint-settings-api.php file we replace with the following:

在解释了这些功能之后,我们现在进入一些实际代码。 我们添加到sitepoint-settings-api.php文件中的先前PHP代码将替换为以下内容:

add_action( 'admin_menu', 'stp_api_add_admin_menu' );
add_action( 'admin_init', 'stp_api_settings_init' );
function stp_api_add_admin_menu( ) {
add_options_page( 'Settings API Page', 'Settings API Page', 'manage_options', 'settings-api-page', 'stp_api_options_page' );
}
function stp_api_settings_init( ) {
register_setting( 'stpPlugin', 'stp_api_settings' );
add_settings_section(
'stp_api_stpPlugin_section',
__( 'Our Section Title', 'wordpress' ),
'stp_api_settings_section_callback',
'stpPlugin'
);
add_settings_field(
'stp_api_text_field_0',
__( 'Our Field 0 Title', 'wordpress' ),
'stp_api_text_field_0_render',
'stpPlugin',
'stp_api_stpPlugin_section'
);
add_settings_field(
'stp_api_select_field_1',
__( 'Our Field 1 Title', 'wordpress' ),
'stp_api_select_field_1_render',
'stpPlugin',
'stp_api_stpPlugin_section'
);
}
function stp_api_text_field_0_render( ) {
$optiOns= get_option( 'stp_api_settings' );
?>
'>
}
function stp_api_select_field_1_render( ) {
$optiOns= get_option( 'stp_api_settings' );
?>

}
function stp_api_settings_section_callback( ) {
echo __( 'This Section Description', 'wordpress' );
}
function stp_api_options_page( ) {
?>

Sitepoint Settings API Admin Page


settings_fields( 'stpPlugin' );
do_settings_sections( 'stpPlugin' );
submit_button();
?>

}

Here we hook the stp_api_settings_init() function to the admin_init hook. There we define and register our settings, sections and fields.

在这里,我们将stp_api_settings_init()函数挂钩到admin_init挂钩。 在那里,我们定义并注册我们的设置,部分和字段。

stp_api_text_field_0_render() and stp_api_select_field_1_render() define HTML output of our two fields, text and select field, both belonging to the same stpPlugin group and stp_api_settings option — or setting — in the wp_options table in the database.

stp_api_text_field_0_render()stp_api_select_field_1_render()定义了我们两个字段(文本和选择字段stp_api_select_field_1_render() HTML输出,它们都属于数据库的wp_options表中的同一个stpPlugin组和stp_api_settings选项(或设置)。

Lastly, we define stp_api_options_page(), which outputs the HTML for our admin options page. We incorporate the settings sections and fields in it. We’ve referred to this function at the top of our file, in the stp_api_add_admin_menu() function, where we registered the admin (options) page.

最后,我们定义stp_api_options_page() ,它为我们的管理选项页面输出HTML。 我们在其中合并了设置部分和字段。 我们在文件顶部的stp_api_add_admin_menu()函数中引用了此函数,在该函数中注册了admin(选项)页面。

When we now go, again, to our settings page, we’ll see that it’s no longer empty:

现在,当我们再次进入设置页面时,我们将看到它不再为空:

Our settings page now has content in it

If we try changing and saving these fields, we will see, upon refresh, that it works! WordPress abstracts away the database transactions for us, nonces, etc.

如果我们尝试更改和保存这些字段,则刷新后会看到它有效! WordPress为我们,nonce等提取数据库事务。

We could further add some validation functionality, further styling of this page, and other things.

我们可以进一步添加一些验证功能,此页面的其他样式以及其他内容。

If we go to WP CLI and try to run wp option get stp_api_settings — after we’ve changed some values for these two fields — we’ll get this:

如果我们转到WP CLI并尝试运行wp option get stp_api_settings (在我们更改了这两个字段的某些值之后),我们将获得以下信息:

The effect of running wp option get stp_api_settings in WP CLI

This shows us that these two fields were saved in our wp_options database as fields of an array, as stp_api_settings setting.

这向我们展示了这两个字段作为stp_api_settings设置保存在wp_options数据库中作为数组的字段。

If we now go to WP CLI shell, we can try out the code we’ll use to fetch these options in our plugin or theme files:

如果现在转到WP CLI shell,我们可以尝试将用于在插件或主题文件中获取这些选项的代码:

Fetching options in our plugin or theme files

结论 (Conclusion)

WordPress has become prominent in the web industry because of its ease of use and its gentle learning curve for developers. The Settings API is one example of this user-friendliness.

WordPress因其易用性和对开发人员的柔和学习曲线而在网络行业中广为人知。 设置API是这种用户友好性的一个示例。

In this guide, we’ve introduced the WordPress Settings API. Much more can be said on this topic, but the intro we’ve presented here should demystify the topic to enable resourceful hackers to build their own solutions starting from this.

在本指南中,我们介绍了WordPress设置API。 关于这个话题,可以说更多的话,但是我们在这里介绍的介绍应该使这个话题神秘化,从而使机智的黑客可以以此为基础构建自己的解决方案。

翻译自: https://www.sitepoint.com/wordpress-settings-api-build-custom-admin-page/


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
author-avatar
太阳之神sqh
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有