Advertisement
Guest User

correct use of add_settings_error in wordpress

a guest
Jul 18th, 2012
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <?php
  2. // Register your settings (this goes in your plugin file or
  3. // if registering theme options, in your functions.php file
  4. function register_my_plugin_options()
  5. {
  6.  
  7. register_setting( 'my_plugin_options', 'my_plugin_options', 'my_plugin_validation_cb' );
  8. add_settings_section( 'my_plugin_main', 'Main Plugin Options', 'my_plugin_main_settings', 'my_plugin_slug' );
  9. add_settings_field( 'send-email-to', 'Send email notices to:', 'my_plugin_main_email_input', 'my_plugin_main', 'my_plugin_slug' );
  10.  
  11. }
  12. add_action( 'admin_init', 'register_my_plugin_options' );
  13.  
  14. function my_plugin_main_settings()
  15. {
  16.   // do stuff here
  17. }
  18.  
  19. function my_plugin_main_email_input()
  20. {
  21.   // you would output your form field here
  22. }
  23.  
  24. // Function for validating all your various settings
  25. function my_plugin_validation_cb( $input )
  26. {
  27.   $valid = array();
  28.   $options = get_option( 'my_plugin_options' );
  29.  
  30.   if( $input['send-email-to'] != sanitize_email( $input['send-email-to'] ) )
  31.   {
  32.     add_settings_error(
  33.       'send-email-to',
  34.       'send-email-field-error',
  35.       "You supplied an invalid email address!",
  36.       'error'
  37.     );
  38.   } else {
  39.     $valid['send-email-to'] = $input['send-email-to'];
  40.   }
  41.  
  42.   return $valid;
  43. }
  44.  
  45. // Example function that would render your options page content
  46. function render_my_plugin_options_page()
  47. {
  48. ?>
  49. <div class="wrap">
  50.   <h2>My Plugin Options</h2>
  51.  
  52. <?php // IMPORTANT!!! If you want your settings fields to display errors,
  53.       // Call settings errors here!
  54.       settings_errors(); ?>
  55.  
  56.   <?php
  57.   // echo out the form and other settings info here
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement