Created
November 27, 2017 15:58
-
-
Save asharirfan/8a1fd37332f6b1f01bb8ba0e1ee667eb to your computer and use it in GitHub Desktop.
WPC β Settings on a Custom Settings page for Tell My Role plugin π―
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 | |
/** | |
* Settings Init. | |
* | |
* Initialize settings for the plugin. | |
* | |
* @since 1.0.0 | |
*/ | |
function tmr_settings_init() { | |
// Register a new section in the "tmr-options-page" page. | |
add_settings_section( | |
'tmr_settings_section', // Section ID. | |
'Tell My Role Settings', // Section Title. | |
'tmr_settings_section_callback', // Section callback function. | |
'tmr-options-page' // Slug of the admin page. | |
); | |
// Register a new field in the "tmr_settings_section" section, inside the "tmr-options-page" page. | |
add_settings_field( | |
'tmr_setting_field', // Setting ID. | |
'Setting Title', // Setting Title | |
'tmr_settings_field_callback', // Setting callback function. | |
'tmr-options-page', // Slug of the admin page. | |
'tmr_settings_section' // Setting section ID. | |
); | |
// Register a new setting. | |
register_setting( 'tmr_settings_group', 'tmr_setting' ); | |
} | |
// Register tmr_settings_init to the admin_init action hook. | |
add_action( 'admin_init', 'tmr_settings_init' ); | |
/** | |
* Section Content Callback. | |
*/ | |
function tmr_settings_section_callback() { | |
echo '<p>Plugin Settings Section</p>'; | |
} | |
/** | |
* Setting Field Content Callback. | |
*/ | |
function tmr_settings_field_callback() { | |
// Get the value of the setting we've registered with register_setting(). | |
$setting = get_option( 'tmr_setting' ); | |
// Output the field. | |
?> | |
<input type="text" id="tmr_setting" name="tmr_setting" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"> | |
<?php | |
} | |
/** | |
* TMR Settings Page. | |
* | |
* Add settings page to WP dashboard menu. | |
* | |
* @since 1.0.0 | |
*/ | |
function tmr_options_page() { | |
// Add top level menu page. | |
add_menu_page( | |
'Tell My Role Settings', | |
'TMR Options', | |
'manage_options', | |
'tmr-options-page', | |
'tmr_options_page_html', | |
'dashicons-admin-generic', | |
90 | |
); | |
} | |
// Register our `tmr_options_page` to the `admin_menu` action hook. | |
add_action( 'admin_menu', 'tmr_options_page' ); | |
/** | |
* TMR Settings Page Content. | |
* | |
* Callback function for TMR menu page. | |
* | |
* @since 1.0.0 | |
*/ | |
function tmr_options_page_html() { | |
// Check user capabilities. | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
?> | |
<div class="wrap"> | |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
<form action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>" method="post"> | |
<?php | |
// Output security fields for the registered setting `tmr_settings_group`. | |
settings_fields( 'tmr_settings_group' ); | |
// Output setting sections and their fields. | |
do_settings_sections( 'tmr-options-page' ); | |
// Output save settings button. | |
submit_button( 'Save Settings' ); | |
?> | |
</form> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment