Created
January 22, 2014 07:22
-
-
Save Zenger/8554729 to your computer and use it in GitHub Desktop.
A class for the lazy to add options to the general screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class sOption { | |
var $field; | |
public function __construct( $name = 'setting', $label = 'Your Setting', $type = 'text', $screen = 'general' ) | |
{ | |
$this->field->name = $name; | |
$this->field->type = $type; | |
$this->field->screen = $screen; | |
$this->field->label = $label; | |
add_filter( 'admin_init' , array( &$this , 'register_fields' ) ); | |
} | |
public function register_fields() | |
{ | |
register_setting( $this->field->screen, $this->field->name, 'esc_attr' ); | |
add_settings_field( $this->field->name, '<label for="'.$this->field->name.'">'.__( $this->field->label , $this->field->name ).'</label>' , array(&$this, 'fields_html') , $this->field->screen ); | |
} | |
public function fields_html() | |
{ | |
$value = get_option( $this->field->name, '' ); | |
switch ($this->field->type) { | |
case 'textarea': | |
echo '<textarea class="regular-text ltr" id="'.$this->field->name.'" name="'.$this->field->name.'">' . $value . '</textarea>'; | |
break; | |
default : | |
echo '<input class="regular-text ltr" type="text" id="'.$this->field->name.'" name="'.$this->field->name.'" value="' . $value . '" />'; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment