Created
May 14, 2014 18:47
-
-
Save bfillmer/e112923393155038b9a7 to your computer and use it in GitHub Desktop.
Generic WP Option Page Functions
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 | |
/** | |
* Generic WordPress Options Panel | |
* | |
* A good starting point for a simple options panel, just | |
* loops through defined options and creates input[type=text] | |
* for each with the value and lets you update. | |
* | |
* Replace namespace- and namespace_ with either your plugin | |
* namespace or class definitions. Also search for instances | |
* of unique- in the below for items that should be changed. | |
*/ | |
// Add our options panel link. | |
add_action('admin_menu', 'namespace_menu_item'); | |
function namespace_menu_item () { | |
// Page Title, Menu Title, User Capability, Menu Slug (Unique), Output Function | |
add_options_page( 'Options', 'Options', 'manage_options', 'namespace-', 'namespace_options_page'); | |
} | |
// Define our database options. Central definition in one place. | |
function namespace_option_definitions() { | |
return array( | |
'option_key' => 'Option Name', | |
); | |
} | |
/** | |
* Retrieve our database options. Note the use of $definitions | |
* as a function call. This allows extending and creating more | |
* than one set of option definitions if needed. | |
*/ | |
function namespace_option_values($definitions=null) { | |
if(!$definitions) return false; | |
// Get our option definitions. | |
$option_definitions = $definitions(); | |
// Return array for current option values. | |
$current_options = array(); | |
// Loop de loop. | |
foreach($option_definitions as $key => $name) { | |
// Store current options in return array. | |
$current_options[$key] = get_option($key); | |
} | |
// Return current option values. | |
return $current_options; | |
} | |
function namespace_options_page() { | |
// Rejection! | |
if (!current_user_can('manage_options')) { | |
wp_die(__('You do not have sufficient permissions to access this page.')); | |
} | |
// Define our options saved in DB. Matches on inputs by name == value. | |
$option_definitions = namespace_option_definitions(); | |
// Display message. | |
$option_alert = false; | |
// Update option values if needed. | |
if(isset($_POST['update-options'])) { | |
foreach($option_definitions as $key => $name) { | |
if(isset($_POST[$key])) { | |
update_option($key, $_POST[$key]); | |
$option_alert = true; | |
} | |
} | |
} | |
$current_options = namespace_option_values('option_definitions'); | |
// General Header stuff. | |
echo '<div class="wrap">'; | |
echo '<h2>'.__('Options', 'unique-slug').'</h2>'; | |
// Options updated feedback. | |
echo $option_alert ? '<div class="updated fade"><p><strong>Options updated.</strong></p></div>' : ''; | |
// Option update form. | |
echo '<form name="unique-form-name" method="post" action="">'; | |
echo '<table class="form-table">'; | |
// Add our options. | |
foreach($current_options as $key => $name) { | |
echo '<tr><th scope="row"><label for="'.$key.'">'.$option_definitions[$key].':</label></th><td><input id="'.$key.'" type="text" name="'.$key.'" value="'.$current_options[$key].'" size="3"></td></tr>'; | |
} | |
// Footer/Close | |
echo '</table>'; | |
echo '<p class="submit"><input type="submit" name="update-options" class="button-primary" value="Save Changes"></p>'; | |
echo '</form>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment