Add custom Wordpress settings


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

This is template for adding custom settings in Wordpress (using settings API).


Copy this code and paste it in your HTML
  1. /**
  2. * Wordpress add custom setting
  3. * Place this in functions.php file of your template.
  4. *
  5. */
  6. function custom_settings() {
  7. ?>
  8. <form method="POST" action="options.php">
  9. <?php settings_fields( 'custom-settings' ); //pass slug name of page, also referred
  10. //to in Settings API as option group name
  11. do_settings_sections( 'custom-settings' ); //pass slug name of page
  12. submit_button();
  13. ?>
  14. </form>
  15. <?php
  16. }
  17.  
  18. // add menu button
  19. add_action('admin_menu', 'plugin_admin_add_page');
  20. function plugin_admin_add_page() {
  21. add_options_page('Custom Plugin Page', 'Custom Plugin Menu', 'manage_options', 'custom-settings', 'custom_settings');
  22. }
  23.  
  24.  
  25. // This tells WordPress to call the function named "setup_theme_admin_menus"
  26. // when it's time to create the menu pages.
  27. // add_action("admin_menu", "setup_theme_admin_menus");
  28.  
  29.  
  30.  
  31. function eg_settings_api_init() {
  32. // Add the section to reading settings so we can add our
  33. // fields to it
  34. add_settings_section(
  35. 'eg_setting_section',
  36. 'Example settings section in reading',
  37. 'eg_setting_section_callback_function',
  38. 'custom-settings'
  39. );
  40.  
  41. // Add the field with the names and function to use for our new
  42. // settings, put it in our new section
  43. add_settings_field(
  44. 'eg_setting_name',
  45. 'Example setting Name',
  46. 'eg_setting_callback_function',
  47. 'custom-settings',
  48. 'eg_setting_section'
  49. );
  50.  
  51. // Register our setting so that $_POST handling is done for us and
  52. // our callback function just has to echo the <input>
  53. register_setting( 'custom-settings', 'eg_setting_name' );
  54. } // eg_settings_api_init()
  55.  
  56. add_action( 'admin_init', 'eg_settings_api_init' );
  57.  
  58.  
  59. // ------------------------------------------------------------------
  60. // Settings section callback function
  61. // ------------------------------------------------------------------
  62. //
  63. // This function is needed if we added a new section. This function
  64. // will be run at the start of our section
  65. //
  66.  
  67. function eg_setting_section_callback_function() {
  68. echo '<p>Intro text for our settings section</p>';
  69. }
  70.  
  71. // ------------------------------------------------------------------
  72. // Callback function for our example setting
  73. // ------------------------------------------------------------------
  74. //
  75. // creates a checkbox true/false option. Other types are surely possible
  76. //
  77.  
  78. function eg_setting_callback_function() {
  79. echo '<input name="eg_setting_name" id="gv_thumbnails_insert_into_excerpt" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'eg_setting_name' ), false ) . ' /> Explanation text';
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.