Skip to content

Instantly share code, notes, and snippets.

@ejdanderson
Created November 1, 2012 20:09
Show Gist options
  • Save ejdanderson/3996147 to your computer and use it in GitHub Desktop.
Save ejdanderson/3996147 to your computer and use it in GitHub Desktop.
WP Options Page Setup with button to run action
<?php
function create_menu() {
add_options_page(__('Plugin Settings', 'ejda'), __('Plugin Settings', 'ejda'), 'manage_options', 'ejda-slug', 'options_page_content');
}
add_action('admin_menu', 'create_menu');
function options_page_content() {
?>
<div class="wrap">
<h2><?php _e('Amazing Plugin Settings', 'ejda'); ?>
</div>
<form method="post">
<input type="submit" value="<?php _e('Submit', 'ejda'); ?>" />
<?php wp_nonce_field('ejda-action', 'ejda-nonce-name'); ?>
<input type="hidden" name="ejda_action" value="give_horse_a_horn" />
</form>
<?php
}
function request_handler() {
if (isset($_POST['ejda_action'])) {
switch ($_POST['ejda_action']) {
case 'give_horse_a_horn':
// Check nonce
check_admin_referer('ejda-action', 'ejda-nonce-name');
do_magic();
break;
default:
break;
}
}
}
// Run after init 10 to catch anyone registering post types and taxonomies at init 10
add_action('init', 'request_handler', 11);
function do_magic() {
// Magic
error_log('There\'s magic going on here');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment