Created
August 11, 2014 16:24
-
-
Save vienhoang/c13fd7e30cb9112583d9 to your computer and use it in GitHub Desktop.
WordPress: Create custom options page
This file contains 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 | |
/* | |
Plugin Name: VH Options | |
Plugin URI: http://vienhoang.com | |
Description: Options page for a theme. | |
Version: 1.0 | |
Author: Vien Hoang | |
Author URI: http://vienhoang.com | |
License: GPL2 | |
*/ | |
class VH_Options { | |
public $options; | |
public function __construct() { | |
// Delete options settings | |
// delete_option( 'vh_plugin_options' ); | |
$this->options = get_option( 'vh_plugin_options' ); | |
$this->register_settings_and_fields(); | |
} | |
public function add_menu_page() { | |
// Last param, WP specific, accept class name and method | |
add_options_page( 'Theme Options', 'Theme Options', 'administrator', __FILE__, array('VH_Options', 'display_options_page') ); | |
} | |
public function display_options_page() { | |
?> | |
<div class="wrap"> | |
<h2>My Theme Options</h2> | |
<form method="post" action="options.php" enctype="multipart/form-data"> | |
<?php | |
// WP security fields | |
settings_fields('vh_plugin_options'); ?> | |
<?php do_settings_sections( __FILE__ ); ?> | |
<p class="submit"> | |
<input name="submit" type="submit" class="button-primary" value="Save Changes" /> | |
</p> | |
</form> | |
</div> | |
<?php | |
} | |
public function register_settings_and_fields() { | |
// Groupname, name, 3rd param optional cb | |
register_setting( 'vh_plugin_options' , 'vh_plugin_options', array($this, 'vh_validate_settings') ); | |
// Id, title of section, cb, which page? | |
add_settings_section( 'vh_main_section', 'Main Settings', array($this, 'vh_main_section_cb'), __FILE__ ); | |
// Attach the banner field to a specific section | |
add_settings_field( 'vh_banner_heading', 'Banner Heading: ', array($this, 'vh_banner_heading_setting'), __FILE__, 'vh_main_section' ); | |
// Attach the logo field to a specific section | |
add_settings_field( 'vh_logo', 'Logo: ', array($this, 'vh_logo_setting'), __FILE__, 'vh_main_section' ); | |
// Attach color scheme drop down list | |
add_settings_field( 'vh_color_scheme', 'Your Desire Color Scheme: ', array($this, 'vh_color_scheme_setting'), __FILE__, 'vh_main_section' ); | |
} | |
public function vh_main_section_cb() { | |
// Optional | |
} | |
public function vh_validate_settings( $plugin_options ) { | |
if ( !empty( $_FILES['vh_logo_upload']['tmp_name'] ) ) { | |
$override = array( 'test_form' => false ); | |
$file = wp_handle_upload( $_FILES['vh_logo_upload'], $override ); | |
$plugin_options['vh_logo'] = $file['url']; | |
} else { | |
$plugin_options['vh_logo'] = $this->options['vh_logo']; | |
} | |
return $plugin_options; | |
} | |
// Inputs | |
// Banner heading | |
public function vh_banner_heading_setting() { | |
// Value gets data from the options | |
echo "<input class='regular-text code' name='vh_plugin_options[vh_banner_heading]' type='text' value='{$this->options['vh_banner_heading']}' />"; | |
} | |
// Logo upload | |
public function vh_logo_setting() { | |
echo '<input class="regular-text code" type="file" name="vh_logo_upload" /><br /><br/>'; | |
if ( isset( $this->options['vh_logo'] ) ) { | |
echo "<img src='{$this->options['vh_logo']}' />"; | |
} | |
} | |
// Color scheme | |
public function vh_color_scheme_setting() { | |
$items = array('Red', 'Green', 'Blue', 'Yellow'); | |
echo "<select name='vh_plugin_options[vh_color_scheme]'>"; | |
foreach ( $items as $item ) { | |
// Check if the selected item is the same in db and store it in a variable | |
$selected = ( $this->options['vh_color_scheme'] === $item) ? 'selected' : ''; | |
echo "<option value='{$item}' $selected>{$item}</option>"; | |
} | |
echo "</select>"; | |
} | |
} | |
add_action( 'admin_menu', function() { | |
VH_Options::add_menu_page(); | |
} ); | |
// When admin loads, create a new instance of class VH_Options to make $this available | |
add_action( 'admin_init', function() { | |
new VH_Options(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment