Categoria: English



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.


WordPress Theming Advices

i18n
Always use i18n functions (like _e() and __()), even if you don't know more languages, so others can do the l10n job for you.

Templates
At least, a template for archives, author, links, search and 404 must be provided.

Alternate CSS file
In your header.php link your style.css and a alternate-style.css in order to allow users to add personal CSS code without fear to loose it in the next theme upgrade.

Supported plugins
Add support to 3rd parties plugins is good. But always check if they exists using function_exists() function to avoid PHP errors.

Time and Date format
Don't use your own time/date format with the_date() and the_time() functions. Use get_option() function, like the_date(get_option('date_format')) and the_time(get_option('time_format')) instead. And remenber, if you call the_date() function twice or more in one page, only the first will echo the date. the_time() function don't have this behavior.

Lightweight
Use WordPress own resources if is possible: i.e. don't add jquery.js, use wp_enqueue_script() function, like wp_enqueue_script('jquery'), and so on...

Widgets
Even if widgets are not your thing (like me), make your theme widget-ready. It's so easy, and sooner or later someone will ask you to do it.


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

    I'd been indicated this excellent website by using this uncle. I am not saying favourable if this send is definitely authored as a result of him since no one recognize these kinds

    venta de pantallas para publicidad @ How add options to your WordPress 2.7…
  2. G

    Buenos dias great article!

    Emily @ Londres: buscar trabajo
  3. G

    Услуга "Взлом почты"!!!!! Это лучший платный сервис взлома почты на заказ. Взлом почты откроет та

    hackerprbx @ RSS Stream
  4. G
  5. G

    Me encanta vuestros blog, lo recomendare a todos mis seguidores, un saludo.

    sillas de paseo @ Copenhaguen en un fin de semana