Skip to content

Instantly share code, notes, and snippets.

@asharirfan
Last active November 24, 2017 13:35
Show Gist options
  • Save asharirfan/464df4cef0f5a88ee9e102aecf93a0ab to your computer and use it in GitHub Desktop.
Save asharirfan/464df4cef0f5a88ee9e102aecf93a0ab to your computer and use it in GitHub Desktop.
WPC — Settings for Tell My Role plugin.
<?php
/**
* Settings Init.
*
* Initialize settings for the plugin.
*
* @since 1.0.0
*/
function tmr_settings_init() {
// Register a new setting for `general` page.
register_setting( 'general', 'tmr_setting' );
// Register a new section in the `general` page.
add_settings_section(
'tmr_settings_section', // Section ID.
'Tell My Role Settings', // Section Title.
'tmr_settings_section_callback', // Section callback function.
'general' // Slug of the admin page.
);
// Register a new field in the `tmr_settings_section` section, inside the `general` page.
add_settings_field(
'tmr_setting_field', // Setting ID.
'Setting Title', // Setting Title
'tmr_settings_field_callback', // Setting callback function.
'general', // Slug of the admin page.
'tmr_settings_section' // Setting section ID.
);
}
// 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" name="tmr_setting" value="<?php isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment