Skip to content

Instantly share code, notes, and snippets.

@gabrysiak
Created October 22, 2014 21:43
Show Gist options
  • Save gabrysiak/d4997b0e516139abffe6 to your computer and use it in GitHub Desktop.
Save gabrysiak/d4997b0e516139abffe6 to your computer and use it in GitHub Desktop.
Wordpress theme options page template.
<?php
add_action( 'admin_menu', 'my_theme_add_admin_menu' );
add_action( 'admin_init', 'my_theme_settings_init' );
function my_theme_add_admin_menu( ) {
add_menu_page( 'Theme Settings', 'My Settings', 'manage_options', 'my_theme_settings', 'my_theme_options_page', 'dashicons-admin-tools', 200 );
}
function my_theme_settings_exist( ) {
if( false == get_option( 'my_theme_settings' ) ) {
add_option( 'my_theme_settings' );
}
}
function my_theme_settings_init( ) {
register_setting( 'pluginPage', 'my_theme_settings' );
add_settings_section(
'my_theme_pluginPage_section',
__( '1337 section description', 'my_theme' ),
'my_theme_settings_section_callback',
'pluginPage'
);
add_settings_field(
'my_theme_text_field_0',
__( '1337 text field description', 'my_theme' ),
'my_theme_text_field_0_render',
'pluginPage',
'my_theme_pluginPage_section'
);
add_settings_field(
'my_theme_checkbox_field_1',
__( '1337 checkbox field description', 'my_theme' ),
'my_theme_checkbox_field_1_render',
'pluginPage',
'my_theme_pluginPage_section'
);
add_settings_field(
'my_theme_radio_field_2',
__( '1337 radio field description', 'my_theme' ),
'my_theme_radio_field_2_render',
'pluginPage',
'my_theme_pluginPage_section'
);
add_settings_field(
'my_theme_textarea_field_3',
__( '1337 textarea field description', 'my_theme' ),
'my_theme_textarea_field_3_render',
'pluginPage',
'my_theme_pluginPage_section'
);
add_settings_field(
'my_theme_select_field_4',
__( '1337 select field description', 'my_theme' ),
'my_theme_select_field_4_render',
'pluginPage',
'my_theme_pluginPage_section'
);
}
function my_theme_text_field_0_render( ) {
$options = get_option( 'my_theme_settings' );
?>
<input type='text' name='my_theme_settings[my_theme_text_field_0]' value='<?php echo $options['my_theme_text_field_0']; ?>'>
<?php
}
function my_theme_checkbox_field_1_render( ) {
$options = get_option( 'my_theme_settings' );
?>
<input type='checkbox' name='my_theme_settings[my_theme_checkbox_field_1]' <?php checked( $options['my_theme_checkbox_field_1'], 1 ); ?> value='1'>
<?php
}
function my_theme_radio_field_2_render( ) {
$options = get_option( 'my_theme_settings' );
?>
<input type='radio' name='my_theme_settings[my_theme_radio_field_2]' <?php checked( $options['my_theme_radio_field_2'], 1 ); ?> value='1'>
<?php
}
function my_theme_textarea_field_3_render( ) {
$options = get_option( 'my_theme_settings' );
?>
<textarea cols='40' rows='5' name='my_theme_settings[my_theme_textarea_field_3]'>
<?php echo $options['my_theme_textarea_field_3']; ?>
</textarea>
<?php
}
function my_theme_select_field_4_render( ) {
$options = get_option( 'my_theme_settings' );
?>
<select name='my_theme_settings[my_theme_select_field_4]'>
<option value='1' <?php selected( $options['my_theme_select_field_4'], 1 ); ?>>Option 1</option>
<option value='2' <?php selected( $options['my_theme_select_field_4'], 2 ); ?>>Option 2</option>
</select>
<?php
}
function my_theme_settings_section_callback( ) {
echo __( 'This section description', 'my_theme' );
}
function my_theme_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>Theme Settings</h2>
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment