Created
January 20, 2013 22:02
-
-
Save esgy/4582096 to your computer and use it in GitHub Desktop.
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: p47 Options | |
* Plugin URI: http://point47.com | |
* Description: demo a options papge | |
* Version: 1.0 | |
* Author: Sorin Gitlan | |
* Author URI: http://point47.com | |
* | |
* Use get_option('p47_plugin_options') in the template to access the saved variables. | |
* | |
*/ | |
class P47_Options{ | |
public $options; | |
public function __construct() | |
{ | |
//delete_option('p47_plugin_options'); | |
//print_r( get_option('p47_plugin_options') ); | |
// get the saved options data | |
$this->options = get_option('p47_plugin_options'); | |
$this->register_settings_and_fields(); | |
} | |
// this is called STATIC | |
public static function add_menu_page() | |
{ | |
add_options_page( 'Theme Options', 'Theme Options', 'administrator', __FILE__, array('P47_Options', 'display_options_page')); | |
} | |
public function display_options_page() | |
{ | |
?> | |
<div class="wrap"> | |
<?php screen_icon(); ?> | |
<h2>My Theme Options</h2> | |
<form action="options.php" method="POST" enctype="multipart/form-data"> | |
<?php settings_fields( 'p47_plugin_options' ); ?> | |
<?php do_settings_sections( __FILE__ ); ?> | |
<p class="submit"> | |
<button type="submit" class="button-primary">Save changes</button> | |
</p> | |
</form> | |
</div> | |
<?php | |
} | |
public function register_settings_and_fields() | |
{ | |
// this registers and initializes the settings | |
register_setting('p47_plugin_options', 'p47_plugin_options', array($this, 'p47_validate_settings') ); | |
// create a section of settings; params: id, title of section, callback, which page | |
add_settings_section( 'p47_main_section', 'Main Settings', array($this, 'p47_main_section_cb'), __FILE__ ); | |
// register form fields | |
add_settings_field( 'p47_banner_heading', 'Banner Heading: ', array($this, 'p47_banner_heading_setting'), __FILE__, 'p47_main_section' ); | |
add_settings_field( 'p47_logo', 'Your Logo: ', array($this, 'p47_logo_setting'), __FILE__, 'p47_main_section' ); | |
add_settings_field( 'p47_color_scheme', 'Your Desired Logo Scheme: ', array($this, 'p47_color_scheme_setting'), __FILE__, 'p47_main_section' ); | |
} | |
public function p47_main_section_cb() | |
{ | |
// optional | |
} | |
public function p47_validate_settings( $plugin_options ) | |
{ | |
// TODO: test if the file is a image | |
// upload the image | |
if( !empty($_FILES['p47_logo_upload']['tmp_name']) ){ | |
$override = array('test_form' => false); | |
$file = wp_handle_upload( $_FILES['p47_logo_upload'], $override ); | |
$plugin_options['p47_logo'] = $file['url']; | |
} else { | |
$plugin_options['p47_logo'] = $this->options['p47_logo']; | |
} | |
return $plugin_options; | |
} | |
/** | |
* | |
* INPUTS | |
* | |
*/ | |
// BANNER Heading | |
public function p47_banner_heading_setting() | |
{ | |
echo '<input type="text" name="p47_plugin_options[p47_banner_heading]" value="'. $this->options['p47_banner_heading'] .'">'; | |
} | |
// LOGO upload | |
public function p47_logo_setting() | |
{ | |
echo '<input type="file" name="p47_logo_upload"><br> <br>'; | |
if( isset($this->options['p47_logo']) ){ | |
echo '<img src="'.$this->options['p47_logo'].'" alt="">'; | |
} | |
} | |
public function p47_color_scheme_setting() | |
{ | |
$items = array('Red', 'Blue', 'Green', 'Black', 'White', 'Yellow'); | |
echo '<select name="p47_plugin_options[p47_color_scheme]">'; | |
foreach ($items as $item) { | |
$selected = ($this->options['p47_color_scheme'] === $item) ? 'selected="selected"' : ''; | |
echo '<option value="'.$item.'" '.$selected.'>'.$item.'</option>'; | |
} | |
echo '</select>'; | |
} | |
} | |
// attach options page only after the admin menu was initiated | |
add_action( 'admin_menu', function(){ | |
P47_Options::add_menu_page(); | |
}); | |
add_action('admin_init', function(){ | |
new P47_Options(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment