Created
December 2, 2012 22:06
-
-
Save ryanburnette/4191266 to your computer and use it in GitHub Desktop.
WordPress Theme Options Plugin
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: My Theme Options | |
Plugin URI: / | |
Description: These are my theme options. | |
Version: 1.0.0 | |
Author: Me | |
Author URI: / | |
*/ | |
function requires_wordpress_version() { | |
global $wp_version; | |
$plugin = plugin_basename( __FILE__ ); | |
$plugin_data = get_plugin_data( __FILE__, false ); | |
if ( version_compare($wp_version, "3.3", "<" ) ) { | |
if( is_plugin_active($plugin) ) { | |
deactivate_plugins( $plugin ); | |
wp_die( "'".$plugin_data['Name']."' requires WordPress 3.3 or higher, and has been deactivated! Please upgrade WordPress and try again.<br /><br />Back to <a href='".admin_url()."'>WordPress admin</a>." ); | |
} | |
} | |
} | |
add_action( 'admin_init', 'requires_wordpress_version' ); | |
// Set-up Action and Filter Hooks | |
register_activation_hook(__FILE__, 'myPrefix_add_defaults'); | |
register_uninstall_hook(__FILE__, 'myPrefix_delete_plugin_options'); | |
add_action('admin_init', 'myPrefix_init' ); | |
add_action('admin_menu', 'myPrefix_add_options_page'); | |
// Delete options table entries ONLY when plugin deactivated AND deleted | |
function myPrefix_delete_plugin_options() { | |
delete_option('myPrefix_options'); | |
} | |
// Define default option settings | |
function myPrefix_add_defaults() { | |
$tmp = get_option('myPrefix_options'); | |
if(($tmp['chk_default_options_db']=='1')||(!is_array($tmp))) { | |
delete_option('myPrefix_options'); // so we don't have to reset all the 'off' checkboxes too! (don't think this is needed but leave for now) | |
$arr = array( "chk_button1" => "1", | |
"chk_button3" => "1", | |
"textarea_one" => "This type of control allows a large amount of information to be entered all at once. Set the 'rows' and 'cols' attributes to set the width and height.", | |
"textarea_two" => "This text area control uses the TinyMCE editor to make it super easy to add formatted content.", | |
"textarea_three" => "Another TinyMCE editor! It is really easy now in WordPress 3.3 to add one or more instances of the built-in WP editor.", | |
"txt_one" => "Enter whatever you like here..", | |
"drp_select_box" => "four", | |
"chk_default_options_db" => "", | |
"rdo_group_one" => "one", | |
"rdo_group_two" => "two" | |
); | |
update_option('myPrefix_options', $arr); | |
} | |
} | |
// Init plugin options to white list our options | |
function myPrefix_init(){ | |
register_setting( 'myPrefix_plugin_options', 'myPrefix_options', 'myPrefix_validate_options' ); | |
} | |
// Add menu page | |
function myPrefix_add_options_page() { | |
add_theme_page('Theme Options', 'Theme Options', 'edit_theme_options', __FILE__, 'myPrefix_render_form'); | |
} | |
// Render the Plugin options form | |
function myPrefix_render_form() { | |
if ( isset($_POST['do_submit']) ) | |
update_option( 'myPrefix_options', $_POST['myPrefix_options'] ); | |
?> | |
<div class="wrap"> | |
<!-- Display Plugin Icon, Header, and Description --> | |
<div class="icon32" id="icon-options-general"><br></div> | |
<h2>Theme Options</h2> | |
<?php if ( $_GET['settings-updated'] == true ) : ?> | |
<div id="setting-error-settings_updated" class="updated settings-error"> | |
<p><strong>Settings saved.</strong></p> | |
</div> | |
<?php endif; ?> | |
<!-- Beginning of the Plugin Options Form --> | |
<form method="post"> | |
<?php settings_fields('myPrefix_plugin_options'); ?> | |
<?php $options = get_option('myPrefix_options'); ?> | |
<!-- Table Structure Containing Form Controls --> | |
<!-- Each Plugin Option Defined on a New Table Row --> | |
<table class="form-table"> | |
<?php if ( $dev ) : // This if statement prevents these items from running. They are here for reference as to available input types. ?> | |
<!-- Text Area Control --> | |
<tr> | |
<th scope="row">Sample Text Area</th> | |
<td> | |
<textarea name="myPrefix_options[textarea_one]" rows="7" cols="50" type='textarea'><?php echo $options['textarea_one']; ?></textarea><br /><span style="color:#666666;margin-left:2px;">Add a comment here to give extra information to Plugin users</span> | |
</td> | |
</tr> | |
<!-- Text Area Using the Built-in WP Editor --> | |
<tr> | |
<th scope="row">Sample Text Area WP Editor 1</th> | |
<td> | |
<?php | |
$args = array("textarea_name" => "myPrefix_options[textarea_two]"); | |
wp_editor( $options['textarea_two'], "myPrefix_options[textarea_two]", $args ); | |
?> | |
<br /><span style="color:#666666;margin-left:2px;">Add a comment here to give extra information to Plugin users</span> | |
</td> | |
</tr> | |
<!-- Text Area Using the Built-in WP Editor --> | |
<tr> | |
<th scope="row">Sample Text Area WP Editor 2</th> | |
<td> | |
<?php | |
$args = array("textarea_name" => "myPrefix_options[textarea_three]"); | |
wp_editor( $options['textarea_three'], "myPrefix_options[textarea_three]", $args ); | |
?> | |
<br /><span style="color:#666666;margin-left:2px;">Add a comment here to give extra information to Plugin users</span> | |
</td> | |
</tr> | |
<!-- Textbox Control --> | |
<tr> | |
<th scope="row">Enter Some Information</th> | |
<td> | |
<input type="text" size="57" name="myPrefix_options[txt_one]" value="<?php echo $options['txt_one']; ?>" /> | |
</td> | |
</tr> | |
<!-- Radio Button Group --> | |
<tr valign="top"> | |
<th scope="row">Radio Button Group #1</th> | |
<td> | |
<!-- First radio button --> | |
<label><input name="myPrefix_options[rdo_group_one]" type="radio" value="one" <?php checked('one', $options['rdo_group_one']); ?> /> Radio Button #1 <span style="color:#666666;margin-left:32px;">[option specific comment could go here]</span></label><br /> | |
<!-- Second radio button --> | |
<label><input name="myPrefix_options[rdo_group_one]" type="radio" value="two" <?php checked('two', $options['rdo_group_one']); ?> /> Radio Button #2 <span style="color:#666666;margin-left:32px;">[option specific comment could go here]</span></label><br /><span style="color:#666666;">General comment to explain more about this Plugin option.</span> | |
</td> | |
</tr> | |
<!-- Checkbox Buttons --> | |
<tr valign="top"> | |
<th scope="row">Group of Checkboxes</th> | |
<td> | |
<!-- First checkbox button --> | |
<label><input name="myPrefix_options[chk_button1]" type="checkbox" value="1" <?php if (isset($options['chk_button1'])) { checked('1', $options['chk_button1']); } ?> /> Checkbox #1</label><br /> | |
<!-- Second checkbox button --> | |
<label><input name="myPrefix_options[chk_button2]" type="checkbox" value="1" <?php if (isset($options['chk_button2'])) { checked('1', $options['chk_button2']); } ?> /> Checkbox #2 <em>(useful extra information can be added here)</em></label><br /> | |
<!-- Third checkbox button --> | |
<label><input name="myPrefix_options[chk_button3]" type="checkbox" value="1" <?php if (isset($options['chk_button3'])) { checked('1', $options['chk_button3']); } ?> /> Checkbox #3 <em>(useful extra information can be added here)</em></label><br /> | |
<!-- Fourth checkbox button --> | |
<label><input name="myPrefix_options[chk_button4]" type="checkbox" value="1" <?php if (isset($options['chk_button4'])) { checked('1', $options['chk_button4']); } ?> /> Checkbox #4 </label><br /> | |
<!-- Fifth checkbox button --> | |
<label><input name="myPrefix_options[chk_button5]" type="checkbox" value="1" <?php if (isset($options['chk_button5'])) { checked('1', $options['chk_button5']); } ?> /> Checkbox #5 </label> | |
</td> | |
</tr> | |
<!-- Another Radio Button Group --> | |
<tr valign="top"> | |
<th scope="row">Radio Button Group #2</th> | |
<td> | |
<!-- First radio button --> | |
<label><input name="myPrefix_options[rdo_group_two]" type="radio" value="one" <?php checked('one', $options['rdo_group_two']); ?> /> Radio Button #1</label><br /> | |
<!-- Second radio button --> | |
<label><input name="myPrefix_options[rdo_group_two]" type="radio" value="two" <?php checked('two', $options['rdo_group_two']); ?> /> Radio Button #2</label><br /> | |
<!-- Third radio button --> | |
<label><input name="myPrefix_options[rdo_group_two]" type="radio" value="three" <?php checked('three', $options['rdo_group_two']); ?> /> Radio Button #3</label> | |
</td> | |
</tr> | |
<!-- Select Drop-Down Control --> | |
<tr> | |
<th scope="row">Sample Select Box</th> | |
<td> | |
<select name='myPrefix_options[drp_select_box]'> | |
<option value='one' <?php selected('one', $options['drp_select_box']); ?>>One</option> | |
<option value='two' <?php selected('two', $options['drp_select_box']); ?>>Two</option> | |
<option value='three' <?php selected('three', $options['drp_select_box']); ?>>Three</option> | |
<option value='four' <?php selected('four', $options['drp_select_box']); ?>>Four</option> | |
<option value='five' <?php selected('five', $options['drp_select_box']); ?>>Five</option> | |
<option value='six' <?php selected('six', $options['drp_select_box']); ?>>Six</option> | |
<option value='seven' <?php selected('seven', $options['drp_select_box']); ?>>Seven</option> | |
<option value='eight' <?php selected('eight', $options['drp_select_box']); ?>>Eight</option> | |
</select> | |
<span style="color:#666666;margin-left:2px;">Add a comment here to explain more about how to use the option above</span> | |
</td> | |
</tr> | |
<tr><td colspan="2"><div style="margin-top:10px;"></div></td></tr> | |
<tr valign="top" style="border-top:#dddddd 1px solid;"> | |
<th scope="row">Database Options</th> | |
<td> | |
<label><input name="myPrefix_options[chk_default_options_db]" type="checkbox" value="1" <?php if (isset($options['chk_default_options_db'])) { checked('1', $options['chk_default_options_db']); } ?> /> Restore defaults upon plugin deactivation/reactivation</label> | |
<br /><span style="color:#666666;margin-left:2px;">Only check this if you want to reset plugin settings upon Plugin reactivation</span> | |
</td> | |
</tr> | |
<?php endif; ?> | |
</table> | |
<p class="submit"> <input type="hidden" name="do_submit" value="1"> | |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> | |
</p> | |
</form> | |
</div> | |
<?php | |
} | |
// Sanitize and validate input. Accepts an array, return a sanitized array. | |
function myPrefix_validate_options($input) { | |
// if we want to sanitize input we can do it here | |
return $input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment