The Settings API

7,608
-1

Published on

The Settings API is a way to create simple, flexible and secure options pages for your WordPress plugins and themes. This presentation was given at WordCamp Sofia 2012, which covers the basics of the Settings API along with some more advanced topics at the end.

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,608
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
70
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

The Settings API

  1. 1. The Settings API Konstantin Kovshenin kovshenin.com
  2. 2. SettingField Section
  3. 3. add_options_page()ref: http://codex.wordpress.org/Function_Reference/add_options_page
  4. 4. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  5. 5. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  6. 6. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  7. 7. add_action( admin_menu, my_admin_menu ); function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page ); }ref: http://codex.wordpress.org/Roles_and_Capabilities
  8. 8. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  9. 9. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  10. 10. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  11. 11. register_setting() add_settings_section() add_settings_field()ref: http://codex.wordpress.org/Settings_API
  12. 12. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  13. 13. add_action( admin_init, my_admin_init ); function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one ); }ref: http://codex.wordpress.org/Function_Reference/register_setting
  14. 14. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}[...]get_option( my-setting );update_option( my-setting, ‘my value’ );
  15. 15. add_action( admin_init, my_admin_init ); function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one ); }ref: http://codex.wordpress.org/Function_Reference/add_settings_section
  16. 16. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  17. 17. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  18. 18. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}[...]add_options_page( ..., my-plugin, ... );
  19. 19. add_action( admin_init, my_admin_init ); function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one ); }ref: http://codex.wordpress.org/Function_Reference/add_settings_field
  20. 20. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  21. 21. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  22. 22. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}[...]add_options_page( ..., my-plugin, ... );
  23. 23. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  24. 24. add_action( admin_init, my_admin_init );function my_admin_init() { register_setting( my-settings-group, my-setting ); add_settings_section( section-one, Section One, section_one_callback, my-plugin ); add_settings_field( field-one, Field One, field_one_callback, my-plugin, section-one );}
  25. 25. function section_one_callback() { echo Some help text goes here.;}
  26. 26. function field_one_callback() { $setting = esc_attr( get_option( my-setting ) ); echo "<input type=text name=my-setting value=$setting />";}[...]register_setting( my-settings-group, my-setting );
  27. 27. add_action( admin_menu, my_admin_menu );function my_admin_menu() { add_options_page( My Plugin, My Plugin, manage_options, my-plugin, my_options_page );}
  28. 28. function my_options_page() { ?> <div class="wrap"> <h2>My Plugin Options</h2> <form action="options.php" method="POST"> <?php settings_fields( my-settings-group ); ?> <?php do_settings_sections( my-plugin ); ?> <?php submit_button(); ?> </form> </div> <?php}
  29. 29. function my_options_page() { ?> <div class="wrap"> <h2>My Plugin Options</h2> <form action="options.php" method="POST"> <?php settings_fields( my-settings-group ); ?> <?php do_settings_sections( my-plugin ); ?> <?php submit_button(); ?> </form> </div> <?php}
  30. 30. function my_options_page() { ?> <div class="wrap"> <h2>My Plugin Options</h2> <form action="options.php" method="POST"> <?php settings_fields( my-settings-group ); ?> <?php do_settings_sections( my-plugin ); ?> <?php submit_button(); ?> </form> </div> <?php}[...]register_setting( my-settings-group, my-setting );
  31. 31. function my_options_page() { ?> <div class="wrap"> <h2>My Plugin Options</h2> <form action="options.php" method="POST"> <?php settings_fields( my-settings-group ); ?> <?php do_settings_sections( my-plugin ); ?> <?php submit_button(); ?> </form> </div> <?php}[...]add_options_page( ..., my-plugin, ... );
  32. 32. function my_options_page() { ?> <div class="wrap"> <h2>My Plugin Options</h2> <form action="options.php" method="POST"> <?php settings_fields( my-settings-group ); ?> <?php do_settings_sections( my-plugin ); ?> <?php submit_button(); ?> </form> </div> <?php }ref: http://codex.wordpress.org/Function_Reference/submit_button
  33. 33. Recap
  34. 34. add_options_page()register_setting()add_settings_section()add_settings_field()settings_fields()do_settings_sections()submit_button()
  35. 35. Using Arrays with the Settings API
  36. 36. register_setting( my-settings-group, color );register_setting( my-settings-group, size );register_setting( my-settings-group, quantity );
  37. 37. register_setting( my-settings-group, my-settings );
  38. 38. function field_one_callback() { $settings = (array) get_option( my-settings ); $color = esc_attr( $settings[color] ); echo "<input type=text name=my-settings[color] value=$color />";}
  39. 39. Sanitization
  40. 40. register_setting( my-settings-group, my-settings, my_settings_sanitize );
  41. 41. function my_settings_sanitize( $input ) { $input[quantity] = absint( $input[quantity] ); return $input;}
  42. 42. Validation
  43. 43. function my_settings_validate( $input ) { $output = get_option( my-settings ); if ( is_email( $input[email] ) ) $output[email] = $input[email]; else add_settings_error( my-settings, invalid-email, You have entered an invalid e-mail address. ); return $output;}
  44. 44. Reusing Controls with the Settings API
  45. 45. add_settings_field( email, E-mail, my_text_input, my-plugin, section-one, array( name => my-settings[email], value => $settings[email], ));
  46. 46. function my_text_input( $args ) { $name = esc_attr( $args[name] ); $value = esc_attr( $args[value] ); echo "<input type=text name=$name value=$value />";}

×