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

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的用...
摘要由CSDN通过智能技术生成

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 ,如下所示:

<?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是一个带有函数名称的字符串,

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值