Created
June 6, 2019 19:08
-
-
Save brianlayman/9ba6ecab9e180dfa1da7d6d98db452c5 to your computer and use it in GitHub Desktop.
A quick WordPress "Tools" menu option to Save the Values and Do the Thing.
This file contains hidden or 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
class THING_DOER | |
{ | |
/** | |
* Holds the values to be used in the fields callbacks | |
*/ | |
private $option_name; | |
private $page_name; | |
private $options; | |
private $action; | |
/** | |
* Start up | |
*/ | |
public function __construct() | |
{ | |
$this->action = 'do_it_now'; | |
$this->option_name = 'thing_doer_option'; | |
$this->page_name = 'do-the-thing'; | |
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); | |
add_action( 'admin_init', array( $this, 'page_init' ) ); | |
} | |
/** | |
* Add options page | |
*/ | |
public function add_plugin_page() | |
{ | |
// This page will be under "Settings" | |
add_management_page( | |
'Thing Doer', | |
'Do the Thing', | |
'manage_options', | |
$this->page_name, | |
array( $this, 'create_admin_page' ) | |
); | |
} | |
/** | |
* Options page callback | |
*/ | |
public function create_admin_page() | |
{ | |
// Set class property | |
$this->options = get_option( $this->option_name ); | |
if (isset($_POST['submit'])) { | |
if ( ! wp_verify_nonce( $_POST[ $this->option_name . '_nonce' ], $this->action ) ) | |
die( '<h1>Invalid nonce.</h1><br/>' . var_export( $_POST, true ) ); | |
$optstr = $_POST[$this->option_name]; | |
update_option( $this->option_name, $optstr); | |
?> | |
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div> | |
<?php | |
$this->options = get_option( $this->option_name ); | |
$this->do_it_now(); | |
} | |
?> | |
<div class="wrap"> | |
<h1>Gonna Do a Thing</h1> | |
<form method="post" name="options" target="_self"> | |
<input type="hidden" name="action" value="<?php echo $this->action; ?>"> | |
<?php wp_nonce_field( $this->action, $this->option_name . '_nonce', FALSE ); ?> | |
<?php | |
// This prints out all hidden setting fields | |
settings_fields( 'do_the_thing_group' ); | |
do_settings_sections( 'my-setting-admin' ); | |
submit_button( 'Do the Thing Now' ); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
/** | |
* Register and add settings | |
*/ | |
public function page_init() | |
{ | |
register_setting( | |
'do_the_thing_group', // Option group | |
$this->option_name, // Option name | |
array( $this, 'sanitize' ) // Sanitize | |
); | |
add_settings_section( | |
'setting_section_id', // ID | |
'Get-R-Done', // | |
array( $this, 'print_section_info' ), // Callback | |
'my-setting-admin' // Page | |
); | |
add_settings_field( | |
'thing_doer_field_one', // ID | |
'Field 1', // thing_doer_field_two | |
array( $this, 'field_one_callback' ), // Callback | |
'my-setting-admin', // Page | |
'setting_section_id' // Section | |
); | |
add_settings_field( | |
'thing_doer_field_two', | |
'Field 2', | |
array( $this, 'field_two_callback' ), | |
'my-setting-admin', | |
'setting_section_id' | |
); | |
} | |
/** | |
* Sanitize each setting field as needed | |
* | |
* @param array $input Contains all settings fields as array keys | |
*/ | |
public function sanitize( $input ) | |
{ | |
$new_input = array(); | |
if( isset( $input['thing_doer_field_one'] ) ) | |
$new_input['thing_doer_field_one'] = absint( $input['thing_doer_field_one'] ); | |
if( isset( $input['thing_doer_field_two'] ) ) | |
$new_input['thing_doer_field_two'] = sanitize_text_field( $input['thing_doer_field_two'] ); | |
return $new_input; | |
} | |
/** | |
* Print the Section text | |
*/ | |
public function print_section_info() | |
{ | |
print 'Enter the stuff needed to do the thing:'; | |
} | |
/** | |
* Get the settings option array and print one of its values | |
*/ | |
public function field_one_callback() | |
{ | |
printf( | |
'<input type="text" id="thing_doer_field_one" name="' . esc_attr( $this->option_name ) . '[thing_doer_field_one]" value="%s" />', | |
isset( $this->options['thing_doer_field_one'] ) ? esc_attr( $this->options['thing_doer_field_one']) : '' | |
); | |
} | |
/** | |
* Get the settings option array and print one of its values | |
*/ | |
public function field_two_callback() | |
{ | |
printf( | |
'<input type="text" id="thing_doer_field_two" name="' . esc_attr( $this->option_name ) . '[thing_doer_field_two]" value="%s" />', | |
isset( $this->options['thing_doer_field_two'] ) ? esc_attr( $this->options['thing_doer_field_two']) : '' | |
); | |
} | |
public function do_it_now() { | |
} | |
} | |
if( is_admin() ) | |
$thing_doer = new THING_DOER(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment