The Settings API
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

The Settings API

  • 2,567 views
Uploaded 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 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.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,567
On Slideshare
1,915
From Embeds
652
Number of Embeds
7

Actions

Shares
Downloads
58
Comments
0
Likes
7

Embeds 652

http://thuthuatwordpress.org 379
http://bheimseinblog.de 201
http://kovshenin.com 54
http://us-w1.rockmelt.com 10
http://lanyrd.com 5
http://hrmtrieu.com 2
http://blog.verha.net 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The Settings API Konstantin Kovshenin kovshenin.com
  • 2. SettingField Section
  • 3. add_options_page()ref: http://codex.wordpress.org/Function_Reference/add_options_page
  • 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. 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. 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. 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. 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. 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. 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. register_setting() add_settings_section() add_settings_field()ref: http://codex.wordpress.org/Settings_API
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. function section_one_callback() { echo Some help text goes here.;}
  • 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. 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. 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. 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. 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. 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. 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. Recap
  • 34. add_options_page()register_setting()add_settings_section()add_settings_field()settings_fields()do_settings_sections()submit_button()
  • 35. Using Arrays with the Settings API
  • 36. register_setting( my-settings-group, color );register_setting( my-settings-group, size );register_setting( my-settings-group, quantity );
  • 37. register_setting( my-settings-group, my-settings );
  • 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. Sanitization
  • 40. register_setting( my-settings-group, my-settings, my_settings_sanitize );
  • 41. function my_settings_sanitize( $input ) { $input[quantity] = absint( $input[quantity] ); return $input;}
  • 42. Validation
  • 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. Reusing Controls with the Settings API
  • 45. add_settings_field( email, E-mail, my_text_input, my-plugin, section-one, array( name => my-settings[email], value => $settings[email], ));
  • 46. function my_text_input( $args ) { $name = esc_attr( $args[name] ); $value = esc_attr( $args[value] ); echo "<input type=text name=$name value=$value />";}