WordPress plugin/theme options page

I use helper methods/functions in my themes and plugins to create the input fields, this was simplified for the example here.

Raw
<?php

class Foo {
  /**
   * Array containing the options
   *
   * @var string
   */
  var $options;

  /**
   * PHP 4 Style constructor which calls the below PHP5 Style Constructor
   *
   * @return none
   */
  function Foo() {
    $this->__construct();
  }

  /**
   * Load options
   *
   * @return none
   */
  function __construct () {
    $this->options = get_option ( 'Foo' );
  } 

  /**
   * Return a specific option value
   *
   * @param string $option name of option to return
   * @return mixed 
   */
  function get_option( $option ) {
    if ( isset ( $this->options[$option] ) )
      return $this->options[$option];
    else
      return false;
  }

}

class FooAdmin extends Foo {
  /**
   * PHP 4 Style constructor which calls the below PHP5 Style Constructor
   *
   * @return none
   */
  function FooAdmin() {
    $this->__construct();
  }

  /**
   * Setup WordPress backend
   *
   * @return none
   */
  function __construct () {
    Foo::__construct ();

    load_plugin_textdomain ( 'foo' , false , 'foo/translations' );

    // Whitelist options
    add_action ( 'admin_init' , array ( &$this , 'register_settings' ) );

    // Activate the options page
    add_action ( 'admin_menu' , array ( &$this , 'add_page' ) ) ;
  }

  /**
   * Whitelist the Foo options
   *
   * @return none
   */
  function register_settings () {
    register_setting( 'Foo_options' , 'Foo' );
  }

  /**
   * Add the options page
   *
   * @return none
   */
  function add_page() {
    if ( current_user_can ( 'manage_options' ) && function_exists ( 'add_options_page' ) ) {
      //add_submenu_page( 'themes.php', 'Foo', 'Foo', 10, 'foo_options', array( &$this, 'admin_page' ) );
      $options_page = add_options_page ( __( 'Foo' , 'foo' ) , __( 'Foo' , 'foo' ) , 'manage_options' , 'Foo' , array ( &$this , 'admin_page' ) );
    }
  }

  /**
   * Output the options page
   *
   * @return none
   */
  function admin_page () { ?>
    <div class="wrap" >
      <h1>Foo Options</h1>
        <form method="post" action="options.php"> <?php
        settings_fields( 'Foo_options' ); ?>
        <input type="hidden" name="Foo[version]" value="<?php echo $this->get_option( 'version' ) ?>" />
        <table class="form-table form-table-clearnone" >

          <tr valign="top">
            <th scope="row">
              Homepage default category
            </th>
            <td> <?php
              $cats = get_categories( 'child_of=0&parent=0' );
              foreach ( $cats as $cat ) { 
                #var_dump( $cat );
                echo '<input name="Foo[front]" ';
                echo 'type="radio" value="' . $cat->term_id . '" ';
                if ( $this->options['front'] == $cat->term_id )
                  echo ' checked="checked" ';
                echo '/> ';
                echo $cat->slug . '<br>';
              } ?>
            </td>
          </tr>

          <tr valign="top">
            <th scope="row">
              Additional homepage categories
            </th>
            <td> <?php
              foreach ( $cats as $cat ) { 
                echo '<input name="Foo[front-additional][' . $cat->term_id . ']" ';
                echo 'type="checkbox" value="1" ';
                if ( $this->options['front-additional'][$cat->term_id] == '1' )
                  echo ' checked="checked" ';
                echo '/> ';
                echo $cat->slug . '<br>';
              } ?>
            </td>
          </tr>

        </table>

        <p class="submit">
          <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
            </p>
        </form>
    
    </div> <?php
  }

}

if ( is_admin () )
  $FooAdmin = new FooAdmin();

2 comments

  1. avatar
    wrote this comment on

    Having the parent class is kinda pointless here, but that's because I took the code from a larger project.

    You should also look into the register_setting() sanitizing callback, and probably build helpers that create the form inputs.

    Code loosely based on <a href="http://sivel.net" rel="nofollow">Sivel's</a> shadowbox-js plugin.

  2. avatar
    wrote this comment on
    I think the code was really good and i liked how you put like refrences on what as going on in the codes. good job! and thank you! :)

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo