用于在管理面板中添加页面和字段的PHP类

时间:2011-03-20 作者:Phil Jackson

我正在尝试构建一个小类,以帮助在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() );  
}
这似乎可行,但当我重新加载页面时,数据尚未保存。

2 个回复
SO网友:kaiser

看看Settings API 第一但也许"OptionTree" 对你的帮助更大。

SO网友:Chip Bennett

我写了一篇教程,可以帮助您:Incorporating the Settings API in WordPress Themes. 我不使用Class方法,但教程可能会有所帮助。

结束

相关推荐

如何在wp_Options中存储带有:n:{{}}语法的选项?

我正在查看一个特定插件是如何工作的,并注意到它以这种数组格式将option\\u value的数据存储在wp\\u options中。。。a:2:{i:20;a:2:{s:8:\"original\";s:15:\"20.original.jpg\";s:9:\"thumbnail\";s:12:\"20.thumb.jpg\";}i:8;a:2:{s:8:\"original\";s:14:\"8.original.png\";s:9:\"thumbnail\";s:11:\"8.thumb.png\"