我正在尝试构建一个小类,以帮助在WordPress管理面板中将一些简单的输入字段构建到自定义页面中,下面是我得到的:
<?php
class create_panel {
public $default_settings = Array(\'blog_banner_image\' => \'main.jpg\');
public function set_ref_name( $str ) {
$this->ref_name = $str;
}
public function set_sidebar_title( $str ) {
$this->side_bar_title = $str;
}
public function create_panel() {
add_action(\'admin_menu\', array(&$this, \'admin_menu\'));
add_action(\'admin_head\', array(&$this, \'admin_head\'));
if ( function_exists( \'register_sidebar\' ) )
register_sidebar( array( \'name\' => \'Sidebar\' ) );
if ( ! is_array( get_option( $this->ref_name ) ) )
add_option( $this->ref_name, $this->default_settings);
$this->options = get_option( $this->ref_name );
}
public function admin_menu() {
add_theme_page(\'Theme Control Panel\', self::$this->side_bar_title, \'edit_themes\', $array_name, array(&$this, \'optionsmenu\'));
}
public function admin_head() {
echo \'<link rel="stylesheet" href="\' . get_bloginfo(\'template_url\') . \'/code/\' . $array_name . \'.css" type="text/css" media="screen" />\';
}
public function set_input_fields( $array ) {
if( ! is_array( $array ) ) {
Throw new Exception(\'set_input_field() perams must be an array.\');
}else{
foreach( $array as $input_arrays ) {
if( count( $input_arrays ) != 3 ) {
Throw new Exception(\'Perams must per input must total 3. input type, name and label\');
}else{
$type = $input_arrays[0];
$name = $input_arrays[1];
$label = $input_arrays[2];
if( ! isset( $this->inputs->{$name} ) ) {
$this->inputs->{$name} = array( \'type\' => $type, \'label\' => $label );
}else{
Throw new Exception(\'Cannot redeclair input perams.\');
}
}
}
}
}
public function optionsmenu() {
if ( $_POST[\'ss_action\'] == \'save\' ) {
if( isset( $this->inputs ) ) {
foreach( $this->inputs as $name => $array ) {
$this->options[(string)$name] = $_POST[$name];
}
update_option( $this->ref_name, $this->options );
echo \'<div class="updated fade" id="message" style="background-color: rgb(255, 251, 204); width: 300px; margin-left: 20px"><p>Settings <strong>saved</strong>.</p></div>\';
}
}
?>
<form action="" method="post" class="themeform">
<input type="hidden" id="ss_action" name="ss_action" value="save">
<h2>Edit Static Text</h2>
<?php
if( isset( $this->inputs ) ) {
foreach( $this->inputs as $name => $array ) {
switch( $array[\'type\'] ) {
case \'text\' :
echo \'<p><label style="display: inline-block; width: 150px; vertical-align: top;">\' . $array[\'label\'] . \'</label><input type="text" name="\' . (string)$name . \'" id="" value="\' . $this->options[(string)$name] . \'" /><p>\';
break;
}
}
}
?>
<p class="submit"><input type="submit" value="Save Changes" name="cp_save"/></p>
</form>
<?php
}
}
try{
$cp = new create_panel();
$cp->set_ref_name( \'styles\' );
$cp->set_sidebar_title( \'Edit Styles\' );
$cp->set_input_fields( array( array( \'text\',\'telephone\',\'Telephone:\' ) ) );
}catch(Exception $err){
die( $err->getMessage() );
}
这似乎可行,但当我重新加载页面时,数据尚未保存。