我已经为我的插件使用设置API有一段时间了,它过去工作得很好。现在它坏了,我不知道为什么。显然,甚至没有调用选项验证函数。不过,表单数据是正确发送和接收的。(两者均由echo
resp公司print_r
调试。)
我总结了一个(希望是)最小但可运行的示例,它既保留了我所做的工作的特点,又保留了我如何做的结构:
myplugin.php
<?php
/*
Plugin Name: My Plugin
Plugin URI: http://wordpress.org/extend/plugins/myplugin/
Description: Example plugin.
Version: 1.0
Author: Your Name
Author URI: http://your.site
License: GPL2
*/
?>
<?php
require_once(\'Plugin.php\');
class MyPlugin extends Plugin {
function __construct() {
parent::__construct(array(\'name\' => \'myplugin\',
\'prettyName\' => \'My Plugin\',
\'mainFile\' => __FILE__));
}
function activate() {
if ( !get_option($this->name) ) {
$options = array( \'test\' => \'testwert\' );
add_option($this->name, $options);
}
}
function deactivate() {}
function options_validate($input) {
$options = get_option($this->name);
$options[\'test\'] = empty($input[\'test\']) ? $options[\'test\'] : $input[\'test\'];
return $options;
}
function setup_settings() {
parent::setup_settings();
include(\'settings.inc.php\');
}
}
new MyPlugin();
?>
Plugin.php
<?php
if ( !class_exists(\'Plugin\') ) {
abstract class Plugin {
protected $name;
protected $prettyName;
protected $mainFile;
private $hasOptions = true;
protected $options;
function __construct($props, $hasOptions = true) {
$this->name = $props[\'name\'];
$this->prettyName = $props[\'prettyName\'];
$this->mainFile = $props[\'mainFile\'];
$this->hasOptions = $hasOptions;
if ( $hasOptions ) {
if ( is_admin() ) {
// Register Options Page
add_action(\'admin_menu\', array(&$this, \'admin_menu_init\'));
// Register settings
add_action(\'admin_init\', array(&$this, \'setup_settings\'));
}
$this->options = get_option($this->name);
}
register_activation_hook($this->mainFile, array(&$this, \'activate\'));
register_deactivation_hook($this->mainFile, array(&$this, \'deactivate\'));
}
function admin_menu_init() {
add_options_page($this->prettyName.\' -- Options\', $this->prettyName, \'manage_options\', basename($this->mainFile), array(&$this, \'options_page\'));
}
abstract function options_validate($input);
function setup_settings() {
register_setting($this->name, $this->name, array(&$this, \'options_validate\'));
}
function options_page() { ?>
<div class="wrap">
<h2><?php echo $this->prettyName; ?> – Settings</h2>
<form action="" method="post">
<?php submit_button(); ?>
<?php settings_fields($this->name); ?>
<?php do_settings_sections($this->name); ?>
<?php submit_button(); ?>
</form>
</div> <?php
}
abstract function activate();
abstract function deactivate();
}
}
?>
settings.inc.php
<?php
class MyPluginCallbacks {
private $name = null;
private $options = null;
function __construct($name, $options) {
$this->name = $name;
$this->options = $options;
}
function general_text() {
echo \'A simple options page demonstration\';
}
function test() {
echo "<input id=\'sanitisation\' name=\'{$this->name}[test]\' type=\'text\' value=\'{$this->options[\'test\']}\'/>\\n";
echo "<br /><small>A simple test value.</small>";
}
}
$callbacks = new MyPluginCallbacks($this->name, $this->options);
add_settings_section(\'general\', \'General\', array(&$callbacks, \'general_text\'), $this->name);
add_settings_field(\'test\', \'Test Value:\', array(&$callbacks, \'test\'), $this->name, \'general\');
?>
可通过测试插件在WP 3.3和最新版本上复制。没有可见的调试消息(启用了WP\\U调试)。
编辑:更改的是选项页面。过去是这样的options.php
但这已经不起作用了。现在是了options-general.php
.