Skip to content

Instantly share code, notes, and snippets.

@Zenger
Created January 22, 2014 07:22
Show Gist options
  • Save Zenger/8554729 to your computer and use it in GitHub Desktop.
Save Zenger/8554729 to your computer and use it in GitHub Desktop.
A class for the lazy to add options to the general screen
<?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