How add options to your WordPress 2.7 dashboard widgets

Well, in this article, I assume you have read the WP Engineer's excellent post Add WordPress Dashboard Widgets, because his code will be our start point.

So, we have this code:

  1.  
  2.  
  3. // Load up the localization file if we're using WordPress in a different language
  4. // Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo"
  5. load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' );
  6.  
  7. /**
  8.  * Content of Dashboard-Widget
  9.  */
  10. function MainFunction() {
  11. echo 'Test Add Dashboard-Widget';
  12. }
  13.  
  14. /**
  15.  * add Dashboard Widget via function wp_add_dashboard_widget()
  16.  */
  17. function MainFunction_Init() {
  18. wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' );
  19. }
  20.  
  21. /**
  22.  * use hook, to integrate new widget
  23.  */
  24. add_action('wp_dashboard_setup', 'MainFunction_Init');
  25.  

OK, now, remenber the wp_add_dashboard_widget function?

  1. function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null )

The $control_callback parameter (obviously optional) is our main target. It gives the 'Configure' option to our spiffy widget.
So, we need to add a 4th parameter to wp_add_dashboard_widget:

  1. wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction', 'MainFunction_Setup');

The MainFunction_Setup function handles two things: the options (set the default options, recolect new values, etc) and the option's presentation (i.e. the HTML part)

So, here goes the code:

  1.  
  2.  
  3. function MainFunction_Options() {
  4. $defaults = array( 'items' => 5, 'boolean' => 1);
  5. if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) )
  6. $options = array();
  7. return array_merge( $defaults, $options );
  8. }
  9.  
  10. function MainFunction_Setup() {
  11.  
  12. $options = MainFunction_Options();
  13.  
  14.  
  15. if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) {
  16. foreach ( array( 'items', 'boolean' ) as $key )
  17. $options[$key] = $_POST[$key];
  18. update_option( 'MainFunction', $options );
  19. }
  20.  
  21. ?>
  22. <p>
  23. <label for="items"><PHP _e('How many items?', 'MainFunction' ); ?>
  24. <select id="items" name="items">
  25. <PHP
  26. for ( $i = 5; $i <= 20; $i = $i + 1 )
  27. echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>";
  28. ?>
  29. </select>
  30. </label>
  31. </p>
  32.  
  33. <p>
  34. <label for="boolean">
  35. <input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> />
  36. <PHP _e('Activate boolean?', 'MainFunction' ); ?>
  37. </label>
  38. </p>
  39. <PHP
  40. }
  41.  

As you can see, MainFunction_Options puts the default values (if necessary, of course) and MainFunction_Setup show the current values and let change them. You can merge this two, of course...

Now, we have to get the options and use them in our MainFunction function. Change MainFunction like that:

  1.  
  2. /**
  3.  * Content of Dashboard-Widget
  4.  */
  5. function MainFunction() {
  6.  
  7. $widget_options = MainFunction_Options();
  8.  
  9. echo 'Test Add Dashboard-Widget<br />';
  10. echo "You have selected $widget_options['items'] items<br />";
  11. echo "boolean is $widget_options['boolean']<br />";
  12.  
  13. }
  14.  

The complete code here:

  1.  
  2. <PHP
  3.  
  4. // Load up the localization file if we're using WordPress in a different language
  5. // Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo"
  6. load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' );
  7.  
  8.  
  9. /**
  10.  * Content of Dashboard-Widget
  11.  */
  12. function MainFunction() {
  13. $widget_options = MainFunction_Options();
  14.  
  15. echo 'Test Add Dashboard-Widget<br />';
  16. echo "You have selected $widget_options['items'] items<br />";
  17. echo "boolean is $widget_options['boolean']<br />";
  18. }
  19.  
  20. /**
  21.  * add Dashboard Widget via function wp_add_dashboard_widget()
  22.  */
  23. function MainFunction_Init() {
  24. wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' , 'MainFunction_Setup' );
  25. }
  26.  
  27. function MainFunction_Options() {
  28. $defaults = array( 'items' => 5, 'boolean' => 1);
  29. if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) )
  30. $options = array();
  31. return array_merge( $defaults, $options );
  32. }
  33.  
  34. function MainFunction_Setup() {
  35.  
  36.  
  37. $options = MainFunction_Options();
  38.  
  39.  
  40. if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) {
  41. foreach ( array( 'items', 'boolean' ) as $key )
  42. $options[$key] = $_POST[$key];
  43. update_option( 'MainFunction', $options );
  44. }
  45.  
  46. ?>
  47. <p>
  48. <label for="items"><PHP _e('How many items?', 'MainFunction' ); ?>
  49. <select id="items" name="items">
  50. <PHP
  51. for ( $i = 5; $i <= 20; $i = $i + 1 )
  52. echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>";
  53. ?>
  54. </select>
  55. </label>
  56. </p>
  57.  
  58. <p>
  59. <label for="boolean">
  60. <input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> />
  61. <PHP _e('Activate boolean?', 'MainFunction' ); ?>
  62. </label>
  63. </p>
  64. <PHP
  65. }
  66.  
  67. /**
  68.  * use hook, to integrate new widget
  69.  */
  70. add_action('wp_dashboard_setup', 'MainFunction_Init');
  71.  
  72. ?>
  73.  

You can view a PHPS version (without PHP crippled tags) here.


Oportunidades

Al iniciarse en la fotografía a un nivel más profesional es recomendable comprar equipo de segunda mano, hasta que uno está seguro de elegir la cámara que más le conviene. Son muchas las páginas donde se puede comprar cámaras de segunda mano, por ejemplo Mundoanuncio.com...

Archivos

Últimos Comentarios

  1. G
  2. G

    It's great to share your views with others.

    Rebs @ Resumen 2012
  3. G
  4. G

    Достаточно интересная и познавательная тема

    Моисей @ Copenhaguen en un fin de semana
  5. G

    It may be true that the law

    Jason Frey @ Silk Smilies Pack