Created
January 27, 2020 12:43
-
-
Save polevaultweb/54b7fc666bcdcd35c2a6096cc0288a99 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Generated by the WordPress Option Page generator | |
* at http://jeremyhixon.com/wp-tools/option-page/ | |
*/ | |
class MyExamplePlugin { | |
private $my_example_plugin_options; | |
public function __construct() { | |
add_action( 'admin_menu', array( $this, 'my_example_plugin_add_plugin_page' ) ); | |
add_action( 'admin_init', array( $this, 'my_example_plugin_page_init' ) ); | |
} | |
public function my_example_plugin_add_plugin_page() { | |
add_options_page( | |
'My Example Plugin', // page_title | |
'My Example Plugin', // menu_title | |
'manage_options', // capability | |
'my-example-plugin', // menu_slug | |
array( $this, 'my_example_plugin_create_admin_page' ) // function | |
); | |
} | |
public function my_example_plugin_create_admin_page() { | |
$this->my_example_plugin_options = get_option( 'my_example_plugin_option_name' ); ?> | |
<div class="wrap"> | |
<h2>My Example Plugin</h2> | |
<p></p> | |
<?php settings_errors(); ?> | |
<form method="post" action="options.php"> | |
<?php | |
settings_fields( 'my_example_plugin_option_group' ); | |
do_settings_sections( 'my-example-plugin-admin' ); | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php } | |
public function my_example_plugin_page_init() { | |
register_setting( | |
'my_example_plugin_option_group', // option_group | |
'my_example_plugin_option_name', // option_name | |
array( $this, 'my_example_plugin_sanitize' ) // sanitize_callback | |
); | |
add_settings_section( | |
'my_example_plugin_setting_section', // id | |
'Settings', // title | |
array( $this, 'my_example_plugin_section_info' ), // callback | |
'my-example-plugin-admin' // page | |
); | |
add_settings_field( | |
'api_key_0', // id | |
'API Key', // title | |
array( $this, 'api_key_0_callback' ), // callback | |
'my-example-plugin-admin', // page | |
'my_example_plugin_setting_section' // section | |
); | |
add_settings_field( | |
'results_limit_1', // id | |
'Results Limit', // title | |
array( $this, 'results_limit_1_callback' ), // callback | |
'my-example-plugin-admin', // page | |
'my_example_plugin_setting_section' // section | |
); | |
add_settings_field( | |
'start_date_2', // id | |
'Start Date', // title | |
array( $this, 'start_date_2_callback' ), // callback | |
'my-example-plugin-admin', // page | |
'my_example_plugin_setting_section' // section | |
); | |
} | |
public function my_example_plugin_sanitize($input) { | |
$sanitary_values = array(); | |
if ( isset( $input['api_key_0'] ) ) { | |
$sanitary_values['api_key_0'] = sanitize_text_field( $input['api_key_0'] ); | |
} | |
if ( isset( $input['results_limit_1'] ) ) { | |
$sanitary_values['results_limit_1'] = sanitize_text_field( $input['results_limit_1'] ); | |
} | |
if ( isset( $input['start_date_2'] ) ) { | |
$sanitary_values['start_date_2'] = sanitize_text_field( $input['start_date_2'] ); | |
} | |
return $sanitary_values; | |
} | |
public function my_example_plugin_section_info() { | |
} | |
public function api_key_0_callback() { | |
printf( | |
'<input class="regular-text" type="text" name="my_example_plugin_option_name[api_key_0]" id="api_key_0" value="%s">', | |
isset( $this->my_example_plugin_options['api_key_0'] ) ? esc_attr( $this->my_example_plugin_options['api_key_0']) : '' | |
); | |
} | |
public function results_limit_1_callback() { | |
printf( | |
'<input class="regular-text" type="text" name="my_example_plugin_option_name[results_limit_1]" id="results_limit_1" value="%s">', | |
isset( $this->my_example_plugin_options['results_limit_1'] ) ? esc_attr( $this->my_example_plugin_options['results_limit_1']) : '' | |
); | |
} | |
public function start_date_2_callback() { | |
printf( | |
'<input class="regular-text" type="text" name="my_example_plugin_option_name[start_date_2]" id="start_date_2" value="%s">', | |
isset( $this->my_example_plugin_options['start_date_2'] ) ? esc_attr( $this->my_example_plugin_options['start_date_2']) : '' | |
); | |
} | |
} | |
if ( is_admin() ) | |
$my_example_plugin = new MyExamplePlugin(); | |
/* | |
* Retrieve this value with: | |
* $my_example_plugin_options = get_option( 'my_example_plugin_option_name' ); // Array of All Options | |
* $api_key_0 = $my_example_plugin_options['api_key_0']; // API Key | |
* $results_limit_1 = $my_example_plugin_options['results_limit_1']; // Results Limit | |
* $start_date_2 = $my_example_plugin_options['start_date_2']; // Start Date | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment