How to create custom theme options in wordpress

Some most of the themes used to custom theme fields like social link, logo etc, So if we can add that option in admin panel we can easily manage the links/fields

First need to create an admin page menu for the theme options page

function ui_theme_menu()
{
add_theme_page( 'Theme Option', 'Social Media', 'manage_options', 'ui_theme_options.php', 'ui_theme_page');
}
add_action('admin_menu', 'ui_theme_menu');

Then register the theme page / create theme page

function ui_theme_page()
{
?>
    <div class="section panel">
      <h1>Social Media Settings</h1>
      <form method="post" enctype="multipart/form-data" action="options.php">
        <?php
          settings_fields('ui_theme_options');
          do_settings_sections('ui_theme_options.php');
        ?>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
            </p>
      </form>
    </div>
    <?php
}
add_action( 'admin_init', 'ui_register_settings' );

Create the fields

 
function ui_register_settings(){
    // Register the settings with Validation callback
    register_setting( 'ui_theme_options', 'ui_theme_options', '' );  
    add_settings_section( 'ui_text_section', '', 'ui_display_section', 'ui_theme_options.php' );
    $field_args = array(
      'type'      => 'text',
      'id'        => 'ui_facebook',
      'name'      => 'ui_facebook',
      'desc'      => 'Link Of Facebook',
      'std'       => '',
      'label_for' => 'ui_facebook',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_facebook', 'Facebook Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args ); 
    $field_args = array(
      'type'      => 'text',
      'id'        => 'ui_twitter',
      'name'      => 'ui_twitter',
      'desc'      => 'Link of Twitter',
      'std'       => '',
      'label_for' => 'ui_twitter',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_twitter', 'Twitter Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_linkedin',
      'name'      => 'ui_linkedin',
      'desc'      => 'Link of Linkedin',
      'std'       => '',
      'label_for' => 'ui_linkedin',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_linkedin', 'Linkedin Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_google_plus',
      'name'      => 'ui_google_plus',
      'desc'      => 'Link of Googleplus',
      'std'       => '',
      'label_for' => 'ui_google_plus',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_google_plus', 'Googleplus Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_youtube',
      'name'      => 'ui_youtube',
      'desc'      => 'Link of Youtube',
      'std'       => '',
      'label_for' => 'ui_youtube',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_youtube', 'Youtube Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_vimeo',
      'name'      => 'ui_vimeo',
      'desc'      => 'Link of Vimeo',
      'std'       => '',
      'label_for' => 'ui_vimeo',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_vimeo', 'Vimeo Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_vimeo',
      'name'      => 'ui_vimeo',
      'desc'      => 'Link of Vimeo',
      'std'       => '',
      'label_for' => 'ui_vimeo',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_vimeo', 'Vimeo Link', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
$field_args = array(
      'type'      => 'text',
      'id'        => 'ui_email',
      'name'      => 'ui_email',
      'desc'      => 'Footer Email',
      'std'       => '',
      'label_for' => 'ui_email',
      'class'     => 'css_class'
    );
    add_settings_field( 'ui_email', 'Footer Email', 'ui_display_setting', 'ui_theme_options.php', 'ui_text_section', $field_args );
}

Disaply the fields in admin page

 
function ui_display_setting($args){
    extract( $args );
    $option_name = 'ui_theme_options';
    $options = get_option( $option_name );
    switch ( $type ) {  
          case 'text':  
              $options[$id] = stripslashes($options[$id]);  
              $options[$id] = esc_attr( $options[$id]);  
              echo "<input class='regular-text$class' type='text' id='$id' name='" . $option_name . "[$id]' value='$options[$id]' />";  
              echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";  
          break;  
    }
}
function ui_display_section($section){ 
}
Now the fields are created in your admin page, you can navigate the pages from themes menu. and use below code in your page.php
$options = get_option( 'ui_theme_options' );
print $options['ui_facebook'];

Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.