Skip to content

Instantly share code, notes, and snippets.

@ryanl4321
Created December 23, 2015 20:06
Show Gist options
  • Save ryanl4321/7982974b3426b3816b0f to your computer and use it in GitHub Desktop.
Save ryanl4321/7982974b3426b3816b0f to your computer and use it in GitHub Desktop.
<?php
class new_general_setting {
private $field_name = false;
private $field_label = false;
private $field_type = false;
function __construct($field_name, $field_label=false, $field_type='text'){
$this->field_name = $field_name;
$this->field_label = $field_label ? $field_label : str_replace('_',' ',$field_name);
$this->field_type = $field_type;
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
function register_fields(){
register_setting( 'general', $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_'.$this->field_type) , 'general' );
}
function fields_text() {
$value = get_option( $this->field_name, '' );
echo '<input type="text" id="'.$this->field_name.'" name="'.$this->field_name.'" value="'.$value.'" style="width:400px;" />';
}
function fields_textarea() {
$value = get_option( $this->field_name, '' );
echo '<textarea id="'.$this->field_name.'" name="'.$this->field_name.'" style="width:400px;height:100px;">'.$value.'</textarea>';
}
}
@ryanl4321
Copy link
Author

This is a quick way to add options to the wordpress general settings page

  1. Just include the file in functions.php
  2. Register settings you want to use
    $about_home_setting = new new_general_setting('about_home', 'Home page about','textarea');
    $about_home_blue_setting = new new_general_setting('about_home_blue', 'Home page blue large text','text');
  3. In a theme you can get the value using:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment