Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RegisteredSettingsTest {
- private $page;
- private $name = 'test-option';
- private $options;
- function __construct() {
- add_action( 'admin_init', array( $this, 'admin_init' ) );
- add_action( 'admin_menu', array( $this, 'admin_menu' ) );
- }
- function admin_init() {
- register_setting( 'setting_ref', $this->name, array( $this, 'update_option' ) );
- $this->options = get_option( $this->name );
- }
- function admin_menu() {
- // change the following line to add_dashboard_page() instead of add_options_page() and the default "Settings saved" message will not appear on submit.
- $this->page = add_options_page( 'Test','Test','manage_options','translatable_demo', array( &$this , 'load' ) );
- }
- function update_option( $data ) {
- $clean = array_map( 'absint', $data );
- unset($data);
- return $clean;
- }
- function load() {
- ?>
- <div class="wrap">
- <?php screen_icon('index'); ?>
- <h2>Test</h2>
- <form action="options.php" method="post">
- <?php settings_fields( 'setting_ref' ); ?>
- <p><label for="<?php $this->field_name( 'fieldone') ?>">Field one <input value="<?php $this->get_option( 'fieldone') ?>" name="<?php $this->field_name( 'fieldone') ?>" type="text" /></label></p>
- <p><label for="<?php $this->field_name( 'fieldtwo') ?>">Field two <input value="<?php $this->get_option( 'fieldtwo') ?>" name="<?php $this->field_name( 'fieldtwo') ?>" type="text" /></label></p>
- <p class="submit"><input type="submit" name="Submit" class="button-secondary action" value="Save" /></p>
- </form>
- <!-- Output options data, so we can see how it currently looks -->
- <pre><?php print_r( $this->options ) ?></pre>
- </div>
- <?php
- }
- function field_name( $name, $echo = true ) {
- $name = "{$this->name}[$name]";
- if( $echo )
- echo $name;
- else
- return $name;
- }
- function get_option( $name, $echo = true ) {
- $val = '';
- if( is_array( $this->options ) && isset( $this->options[$name] ) )
- $val = $this->options[$name];
- if( $echo )
- echo $val;
- else
- return $val;
- }
- }
- $r = new RegisteredSettingsTest;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement