Advertisement
pjeaje

How To Create A Simple WordPress Theme Options Page

Apr 26th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.37 KB | None | 0 0
  1. // THEME SETTINGS PAGE - http://www.wpexplorer.com/wordpress-theme-options/
  2. //register settings
  3. function theme_settings_init(){
  4.     register_setting( 'theme_settings', 'theme_settings' );
  5. }
  6.  
  7. //add settings page to menu
  8. function add_settings_page() {
  9. add_menu_page( __( 'Theme Settings' ), __( 'Theme Settings' ), 'manage_options', 'settings', 'theme_settings_page');
  10. }
  11.  
  12. //add actions
  13. add_action( 'admin_init', 'theme_settings_init' );
  14. add_action( 'admin_menu', 'add_settings_page' );
  15.  
  16. //define your variables
  17. $item = array('default','blue','green',);
  18.  
  19. //start settings page
  20. function theme_settings_page() {
  21.  
  22. if ( ! isset( $_REQUEST['updated'] ) )
  23. $_REQUEST['updated'] = false;
  24.  
  25. //get variables outside scope
  26. global $item;
  27. ?>
  28.  
  29. <div>
  30.  
  31. <div id="icon-options-general"></div>
  32. <h2><?php _e( 'Theme Settings' ) //your admin panel title ?></h2>
  33. <p>Visit <a href="http://www.wpexplorer.com/wordpress-theme-options/">HERE</a> to see how to use it.</p>
  34.  
  35.  
  36. <?php
  37. //show saved options message
  38. if ( false !== $_REQUEST['updated'] ) : ?>
  39. <div><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
  40. <?php endif; ?>
  41.  
  42. <form method="post" action="options.php">
  43.  
  44. <?php settings_fields( 'theme_settings' ); ?>
  45. <?php $options = get_option( 'theme_settings' ); ?>
  46.  
  47. <table>
  48.  
  49. <!-- Option 1: Custom Logo -->
  50. <tr valign="top">
  51. <th scope="row"><?php _e( 'Text' ); ?></th>
  52. <td><input id="theme_settings[your-text]" type="text" size="36" name="theme_settings[your-text]" value="<?php esc_attr_e( $options['your-text'] ); ?>" />
  53. <label for="theme_settings[your-text]"><?php _e( 'Enter your text' ); ?></label></td>
  54. </tr>
  55.  
  56. <!-- Option 2: Color Scheme -->
  57. <tr valign="top">
  58. <th scope="row"><?php _e( 'Choice' ); ?></th>
  59. <td><select name="theme_settings[item]">
  60. <?php foreach ($item as $option) { ?>
  61. <option <?php if ($options['item'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
  62. <?php } ?>
  63. </select>                    
  64. <label for="theme_settings[item]"><?php _e( 'Choose your item' ); ?></label></td>
  65. </tr>
  66.  
  67. <!-- Option 3: Disable Footer -->
  68. <tr valign="top">
  69. <th scope="row"><?php _e( 'Disable' ); ?></th>
  70. <td><input id="theme_settings[disable]" name="theme_settings[disable]" type="checkbox" value="1" <?php checked( '1', $options['disable'] ); ?> />
  71. <label for="theme_settings[disable]"><?php _e( 'Check this box if you would like to disable the widgetized footer section' ); ?></label></td>
  72. </tr>
  73.  
  74. <!-- Option 4: Tracking Code -->
  75. <tr valign="top">
  76. <th scope="row"><?php _e( 'textbox' ); ?></th>
  77. <td><label for="theme_settings[textbox]"><?php _e( 'Enter your long text' ); ?></label>
  78. <br />
  79. <textarea id="theme_settings[textbox]" name="theme_settings[textbox]" rows="5" cols="36"><?php esc_attr_e( $options['textbox'] ); ?></textarea></td>
  80. </tr>
  81.  
  82. </table>
  83.  
  84. <p><input name="submit" id="submit" value="Save Changes" type="submit"></p>
  85. </form>
  86.  
  87. </div><!-- END wrap -->
  88. <h1>How to use it</h1>
  89. <p>Insert the following at the top of the theme file you are editing.</p>
  90. <code>
  91. &#60;&#63;php &#36;options &#61; get&#95;option&#40; &#39;theme&#95;settings&#39; &#41;&#59;
  92. &#63;&#62;
  93. </code>
  94.  
  95. <h2>Adding Text</h2>
  96. <p>In order to add this to your theme you will first check for the option and if it’s blank show nothing instead.</p>
  97. <code>
  98. &#60;&#63;php if&#40;&#36;options&#91;&#39;your&#45;text&#39;&#93;&#41; &#123; &#63;&#62;<br />
  99. &#60;&#63;php echo &#36;options&#91;&#39;your&#45;text&#39;&#93;&#59;
  100. &#63;&#62;<br />
  101. &#60;&#63;php &#125; else &#123; &#63;&#62;<br />
  102. &#60;&#63;php &#125;
  103. &#63;&#62;
  104. </code>
  105.  
  106. <h2>Adding Choice</h2>
  107. <p>In the choice code above we added some options in the theme options page where a user could choose from a drop-down their choice.</p>
  108. <code>
  109. &#60;&#63;php if&#40;&#36;options&#91;&#39;item&#39;&#93; &#33;&#61; &#39;&#39;&#41; &#123; &#63;&#62;<br />
  110. &#60;&#63;php
  111. echo &#36;options&#91;&#39;item&#39;&#93;&#59; &#63;&#62;<br />
  112. &#60;&#63;php &#125; &#63;&#62;
  113. </code>
  114.  
  115. <h2>Adding Disable</h2>
  116. <p>A common option is the ability to disable something in your theme. Using the admin panel code above you would then add the conditional to wherever the code for what you want disabled is.</p>
  117. <code>
  118. &#60;&#63;php if &#40;&#36;options&#91;&#39;disable&#39;&#93; &#33;&#61; true&#41; &#123;
  119. &#63;&#62;<br />
  120. What ever is in here gets
  121. disabled when the theme options checkbox
  122. is ticked<br />
  123. &#60;&#63;php &#125; &#63;&#62;
  124. </code>
  125.  
  126. <h2>Adding Text box</h2>
  127. <p>This is a simple textarea input where the user will paste their text so all you need to do is “echo” the option’s input in your theme.</p>
  128. <code>
  129. &#60;&#63;php<br />
  130. &#47;&#47;show tracking code for the header<br />
  131. echo
  132. stripslashes&#40;&#36;options&#91;&#39;textbox&#39;&#93;&#41;&#59;<br />
  133. &#63;&#62;
  134. </code>
  135.  
  136.  
  137.  
  138. <?php
  139. }
  140. //sanitize and validate
  141. function options_validate( $input ) {
  142.     global $select_options, $radio_options;
  143.     if ( ! isset( $input['option1'] ) )
  144.         $input['option1'] = null;
  145.     $input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
  146.     $input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
  147.     if ( ! isset( $input['radioinput'] ) )
  148.         $input['radioinput'] = null;
  149.     if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
  150.         $input['radioinput'] = null;
  151.     $input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
  152.     return $input;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement