Created
August 23, 2019 06:15
-
-
Save RadGH/85ec4c8fec53d2ff4711170e54e19425 to your computer and use it in GitHub Desktop.
WordPress settings api for simple settings fields
This file contains 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 | |
add_action( 'admin_menu', 'myplugin_register_settings_menu_page' ); | |
add_action( 'admin_init', 'myplugin_register_settings_fields' ); | |
function myplugin_register_settings_menu_page() { | |
add_options_page( 'My plugin', 'My plugin', 'manage_options', 'myplugin-settings', 'myplugin_display_settings_menu' ); | |
} | |
function myplugin_register_settings_fields() { | |
add_settings_section( 'myplugin-settings-analytics', 'Google Analytics Settings', null, 'myplugin-settings' ); | |
// Tracking ID | |
add_settings_field( | |
'myplugin_google_analytics_tracking_id', | |
'Tracking ID', | |
'myplugin_setting_field_text', | |
'myplugin-settings', | |
'myplugin-settings-analytics', | |
array( | |
'option_name' => 'myplugin_google_analytics_tracking_id', | |
'description' => 'Find your Tracking ID in your <a href="https://analytics.google.com/" target="_blank">Analytics</a> settings under the Property Settings tab.', | |
) | |
); | |
register_setting( 'myplugin_setting_fields', 'myplugin_google_analytics_tracking_id', array( 'sanitize_callback' => 'myplugin_validate_settings' ) ); | |
// View ID | |
add_settings_field( | |
'myplugin_google_analytics_view_id', | |
'View ID', | |
'myplugin_setting_field_text', | |
'myplugin-settings', | |
'myplugin-settings-analytics', | |
array( | |
'option_name' => 'myplugin_google_analytics_view_id', | |
'description' => 'Find your View ID in the Analytics admin area under View > View Settings.', | |
) | |
); | |
register_setting( 'myplugin_setting_fields', 'myplugin_google_analytics_view_id', array( 'sanitize_callback' => 'myplugin_validate_settings' ) ); | |
// API Key | |
add_settings_field( | |
'myplugin_google_analytics_api_key', | |
'API Key', | |
'myplugin_setting_field_text', | |
'myplugin-settings', | |
'myplugin-settings-analytics', | |
array( | |
'option_name' => 'myplugin_google_analytics_api_key', | |
'description' => 'For information on creating a Google Analytics API Key, <a href="https://developers.google.com/analytics/devguides/reporting/core/v4/authorization" target="_blank">click here</a>. Once you have an API key, be sure to share your Analytics account access with the service account email (see <a href="https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php" target="_blank">Reporting API 4: Enable the API</a>).', | |
) | |
); | |
register_setting( 'myplugin_setting_fields', 'myplugin_google_analytics_api_key', array( 'sanitize_callback' => 'myplugin_validate_settings' ) ); | |
} | |
function myplugin_validate_settings( $input ) { | |
// Check our textbox option field contains no HTML tags - if so strip them out | |
// $input['text_string'] = wp_filter_nohtml_kses( $input['text_string'] ); | |
return $input; // return validated input | |
} | |
function myplugin_setting_field_text( $args ) { | |
$option_name = isset($args['option_name']) ? $args['option_name'] : false; | |
if ( !$option_name ) return; | |
$id = isset($args['id']) ? $args['id'] : $option_name; | |
$type = isset($args['type']) ? $args['type'] : 'text'; | |
$name = isset($args['field_name']) ? $args['field_name'] : $option_name; | |
$value = get_option( $args['option_name'] ); | |
$atts = array( | |
'id' => $id, | |
'name' => $name, | |
'type' => $type, | |
'value' => $value, | |
'size' => 60, | |
); | |
echo '<input '; | |
foreach( $atts as $k => $v ) echo $k . '="' . esc_attr($v) . '" '; | |
echo '/>'; | |
if ( isset($args['description']) ) { | |
echo '<div class="description">', wpautop($args['description']), '</div>'; | |
} | |
} | |
function myplugin_display_settings_menu() { | |
?> | |
<div class="wrap"> | |
<h2>My Plugin Settings</h2> | |
<form action="options.php" method="post"> | |
<?php wp_nonce_field( 'myplugin-save-settings' ); ?> | |
<?php settings_fields( 'myplugin_setting_fields' ); ?> | |
<?php do_settings_sections( 'myplugin-settings' ); ?> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment