Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to build a little class to help build some simple input fields into a custom page in WordPress admin panel, here's what I've got:

<?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() );  
}

it seems to work but then when i reload the page the data has not saved.

share|improve this question
add comment

migrated from stackoverflow.com Mar 20 '11 at 23:42

This question came from our site for professional and enthusiast programmers.

2 Answers

Take a look at the Settings API first. But maybe "OptionTree" will help you even more.

share|improve this answer
    
Option Tree is very cool. Haven't known about it. Thank you. –  Rilwis Mar 21 '11 at 7:39
    
@Rilwis: You're welcome. Btw: Your meta box class is kool too. –  kaiser Mar 21 '11 at 7:47
add comment

I wrote a tutorial that may help you: Incorporating the Settings API in WordPress Themes. I don't use the Class method, but perhaps the tutorial may be helpful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.