My Theme Optons Page


/ Published in: PHP
Save to your folder(s)

The Logo settings start on line 110 of the commented version.


Copy this code and paste it in your HTML
  1. <?php
  2. // **********************************************************
  3. // **********************************************************
  4. // *** MY THEME OPTIONS PAGE A WORKING SAMPLE TO BUILD ON ***
  5. // **********************************************************
  6. // **********************************************************
  7. ?>
  8. <?php
  9. // **************************************************************
  10. // **************************************************************
  11. // **** CODE TO ADD TO THE CURRENT THEMES FUNCTIONS.PHP FILE ****
  12. // **************************************************************
  13. // **************************************************************
  14. include_once( dirname( __FILE__ ) . '/theme-options.php' );
  15. ?>
  16. <?php
  17. // *************************************************************
  18. // *************************************************************
  19. // **** CODE TO ADD TO THE THEME FILE WHERE OPTIONS DISPLAY ****
  20. // *************************************************************
  21. // *************************************************************
  22. // **** CHANGE THE KEY TO THE OPTION THAT YOU WANT TO SHOW *****
  23. // *************************************************************
  24. // *************************************************************
  25. /**
  26.  * <?php $options = get_option('plugin_options'); ?>
  27.  * <img src="<?php echo $options['logo']; ?>" alt="Logo" />
  28.  *
  29.  */
  30. ?>
  31. <?php
  32. // **************************************************************************
  33. // **************************************************************************
  34. // ********* IN THIS CASE YOU CAN STYLE THE THEME OPTIONS WITH THIS *********
  35. // **************************************************************************
  36. // **************************************************************************
  37. // **** THE OPTIONS_PAGE.CSS SHOULD GO IN THEME DIR/CSS/options_page.css ****
  38. // **************************************************************************
  39. // **************************************************************************
  40. ?>
  41. <?php
  42. // *****************************************************************
  43. // *****************************************************************
  44. // ************ THE ACTUAL OPTIONS.PHP FILE CODE *******************
  45. // *****************************************************************
  46. // *****************************************************************
  47. add_action('admin_menu', 'create_theme_options_page');
  48. add_action('admin_init', 'register_and_build_fields');
  49. function create_theme_options_page() {
  50. add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, 'options_page_fn');
  51. }
  52. function register_and_build_fields() {
  53. register_setting('plugin_options', 'plugin_options', 'validate_setting');
  54. add_settings_section('main_section', 'Main Settings', 'section_cb', __FILE__);
  55. add_settings_field('color_scheme', 'Color Scheme:', 'color_scheme_setting', __FILE__, 'main_section');
  56. add_settings_field('logo', 'Upload Custom Logo:', 'logo_setting', __FILE__, 'main_section'); // LOGO
  57. add_settings_field('banner_heading', 'Banner Heading:', 'banner_heading_setting', __FILE__, 'main_section');
  58. add_settings_field('adverting_information', 'Advertising Info:', 'advertising_information_setting', __FILE__, 'main_section');
  59. add_settings_field('ad_one', 'Ad:', 'ad_setting_one', __FILE__, 'main_section'); // Ad1
  60. add_settings_field('ad_two', 'Second Ad:', 'ad_setting_two', __FILE__, 'main_section'); // Ad2
  61. }
  62. function options_page_fn() {
  63. ?>
  64. <div id="theme-options-wrap" class="widefat">
  65. <div class="icon32" id="icon-tools"></div>
  66. <h2>IdeaTree Designs Theme Options Panel</h2>
  67. <p>Use the sections below to customize your theme, then click Save Changes.</p>
  68. <form method="post" action="options.php" enctype="multipart/form-data">
  69. <?php settings_fields('plugin_options'); ?>
  70. <?php do_settings_sections(__FILE__); ?>
  71. <p class="submit">
  72. <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
  73. </p>
  74. </form>
  75. </div>
  76. <?php
  77. }
  78. // Banner Heading
  79. function banner_heading_setting() {
  80. $options = get_option('plugin_options');
  81. echo "<input name='plugin_options[banner_heading]' type='text' value='{$options['banner_heading']}' />";
  82. }
  83. // Color Scheme
  84. function color_scheme_setting() {
  85. $options = get_option('plugin_options');
  86. $items = array("Red", "Green", "Blue");
  87. echo "<select name='plugin_options[color_scheme]'>";
  88. foreach ($items as $item) {
  89. $selected = ( $options['color_scheme'] === $item ) ? 'selected = "selected"' : '';
  90. echo "<option value='$item' $selected>$item</option>";
  91. }
  92. echo "</select>";
  93. }
  94. // Advertising info
  95. function advertising_information_setting() {
  96. $options = get_option('plugin_options');
  97. echo "<textarea name='plugin_options[advertising_information]' rows='10' cols='60' type='textarea'>{$options['advertising_information']}</textarea>";
  98. }
  99. // Ad one
  100. function ad_setting_one() {
  101. echo '<input type="file" name="ad_one" />';
  102. }
  103. // Ad two
  104. function ad_setting_two() {
  105. echo '<input type="file" name="ad_two" />';
  106. }
  107. /**
  108.  * Logo
  109.  */
  110. function logo_setting() {
  111. echo '<input type="file" name="logo" />';
  112. }
  113. function validate_setting($plugin_options) {
  114. $keys = array_keys($_FILES);
  115. $i = 0;
  116. foreach ($_FILES as $image) {
  117. // if a files was upload
  118. if ($image['size']) {
  119. // if it is an image
  120. if (preg_match('/(jpg|jpeg|png|gif)$/', $image['type'])) {
  121. $override = array('test_form' => false);
  122. $file = wp_handle_upload($image, $override);
  123. $plugin_options[$keys[$i]] = $file['url'];
  124. } else {
  125. $options = get_option('plugin_options');
  126. $plugin_options[$keys[$i]] = $options[$logo];
  127. wp_die('No image was uploaded.');
  128. }
  129. }
  130. // else, retain the image that's already on file.
  131. else {
  132. $options = get_option('plugin_options');
  133. $plugin_options[$keys[$i]] = $options[$keys[$i]];
  134. }
  135. $i++;
  136. }
  137. return $plugin_options;
  138. }
  139. function section_cb() {}
  140. // Add stylesheet
  141. add_action('admin_head', 'admin_register_head');
  142. function admin_register_head() {
  143. $url = get_bloginfo('template_directory') . '/css/options_page.css';
  144. echo "<link rel='stylesheet' href='$url' />\n";
  145. }
  146. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.