首先,我要说我几乎不使用WordPress,实际上,我最后一次在WordPress中创建网站是在2.2期间。昨天我把所有东西弄得一团糟,在这里问了几个问题,试图使基本的菜单插件正常工作。
现在,我的插件可以正常运行,并且性能完全符合我的预期,因此我决定在此进行一些小的更改以增加功能和兼容性-包括使用Settings API。但是,阅读此API教程的时间很短,我感到非常困惑,然后,当我继续阅读并尝试实现示例时,这种困惑就加深了-由于我的插件被实现为类,这变得更加困难。
除非我做错了事,否则据我所知,要使用Settings API,需要创建一个新的功能PER SETTING。这意味着平均每个插件需要3-5个功能,而更多高级插件则需要多达数百个功能。当您可以很容易地将所有适用的$_POST
变量导入数组并放弃整个混乱时,编写这么多函数(开发一个命名系统以免混淆)似乎很荒谬。
也许我是过时的,但是除非能从中受益,否则我看不出将我编写的代码增加三倍或四倍的原因。这是在尝试添加Settings API之前我如何管理选项的方法:
function __construct() {
/* constructor stuff */
$this->options = $this->db_options = get_option( 'de-menu-options' );
if( $this->options === false ){
$this->options = $this->defaults;
}
if (is_admin()) {
add_action('admin_menu', array(&$this, 'admin_menu'));
}
/* more stuff */
// When WordPress shuts down we store changes to options
add_action('shutdown', array(&$this, 'update'));
}
public function admin_menu() {
add_options_page('DE Menu Options', 'DE Menu', 'manage_options', 'de-menu-options', array(&$this, 'options'));
add_option('de-menu-options', $this->options);
}
public function options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
if ( !empty($_POST) && check_admin_referer('de-menu-options') ) {
// These options are saved to the database at shutdown
$this->options = array(
"columns" => $_POST["de-menu-columns"],
"maintenance" => $_POST["de-menu-maintenance"]
);
echo 'DE Menu options saved';
}
?>
<div class="wrap">
<h2>DE Menu Plugin</h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<?php settings_fields('de-menu-options'); ?>
<input type="checkbox" name="de-menu-maintenance" />
<label for="de-menu-columns">Columns:</label>
<input type="text" name="de-menu-columns" value="<?php echo $this->options['columns']; ?>" />
<p class="submit">
<input type="submit" name="de-menu-submit" value="Update Options »" />
</p>
</form>
</div>
<?php
}
function update() {
// By storing all changes at the end we avoid multiple database calls
$diff = array_diff( $this->options, $this->db_options );
if( !empty( $diff ) ){
update_option('de-menu-options', $this->options);
}
}
现在,使用设置API,我将获得以下更多信息:
function __construct() {
/* constructor stuff */
// Do I load options? Will they be loaded for me? Who knows?
if (is_admin()) {
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('admin_init', array(&$this, 'admin_init'));
}
/* more stuff */
// Settings API should update options for me... I think
}
public function admin_menu() {
add_options_page('DE Menu Options', 'DE Menu', 'manage_options', 'de-menu-options', array(&$this, 'options'));
add_option('de-menu-options', $this->options);
}
public function admin_init() {
register_setting('de-menu-options','de-menu-options',array(&$this,'validate'));
add_settings_section('de-menu-main-options', 'Main Settings', 'options_section', 'de-menu-options');
add_settings_field('de-menu-maintenance', 'Maintenance Mode', array(&$this,'options_maintenance'), 'de-menu-options', 'de-menu-main-options');
add_settings_field('de-menu-columns', 'Columns', array(&$this,'options_columns'), 'de-menu-options', 'de-menu-main-options');
}
public function options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
if ( !empty($_POST) && check_admin_referer('de-menu-options') ) {
// These options are saved to the database at shutdown
$this->options = array(
"columns" => $_POST["de-menu-columns"],
"maintenance" => $_POST["de-menu-maintenance"]
);
echo 'DE Menu options saved';
}
?>
<div class="wrap">
<h2>DE Menu Plugin</h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<?php settings_fields('de-menu-options'); ?>
<?php do_settings_sections('de-menu-options'); ?>
<p class="submit">
<input type="submit" name="de-menu-submit" value="Update Options »" />
</p>
</form>
</div>
<?php
}
public function options_section() {
echo '<p>' . __('Main description of this section here.','de-menu-lang') . '</p>';
}
public function options_maintenance() {
echo "<input id='de-menu-maintenance' name='options[maintenance]' type='checkbox' />";
}
public function options_columns() {
echo "<input id='de-menu-columns' name='options[columns]' type='checkbox' value=".$this->options['columns']."/>";
}
function validate($options) {
return $options; // I guess?
}
从滚动条可能很痛苦地看出,只有两个选项,代码已经更长了。从评论中可以看出,我并不完全了解自己在做什么。然后就是要有5个新功能(并且只删除1个)来完成所有这些工作。
那么,我从所有这些额外工作中获得了什么好处呢?
add_settings_section
和也可以使用API add_settings_field
,这两个函数比任何东西都给代码增加了膨胀,避免了这些,并且避免了膨胀。