3

I've created a dashboard widget in my functions.php file. I want to have it be able to update some stuff in the database upon form submittal. According to the codex documentation, I should be handling my post submissions with the 4th argument of wp_add_dashboard_widget(), $control_callback.

So I've created my handler function and included it in my functions.php file. When I have my form submit, nothing happens. I thought about it for a minute, and then I hooked the callback function to wp_dashboard_setup with add_action(); My call back function now runs, but its doing so regardless of whether I pass the control_callback function to wp_add_dashboard_widget() making me feel like my solution is kind of hacky.

Am I doing this wrong and is there a better way to do tthis? Also, is my form supposed to submit anywhere fancy or can it just submit right back to where its drawn?

Heres my full code:

function custom_dashboard_widget_coach() {
        echo "
        <p><strong>Finalize Game</strong></p>
        <form method='post'>
        <div class='team_class_wrap'>
            <label>Class</label>
            <select name='team' id='team'>
                <option value='5a'>5A</option>
                <option value='4a'>4A</option>
                <option value='3a'>3A</option>
                <option value='2a'>2A</option>
            </select>
        </div>          
        <input type='submit' value='Report Game Result' />
        </form>
        ";
    }

    function custom_dashboard_widget_coach_handle()
    {
        var_dump($_POST);exit;

    }

    function add_custom_dashboard_widget_coach() {
        wp_add_dashboard_widget('custom_dashboard_widget_coach', 'My Team', 'custom_dashboard_widget_coach', 'custom_dashboard_widget_coach_handle');
    }
    add_action('wp_dashboard_setup', 'custom_dashboard_widget_coach_handle');
    add_action('wp_dashboard_setup', 'add_custom_dashboard_widget_coach');

1 Answer 1

5

Actually the from fields only should be in the control_callback function , both form and handling so try this:

<?php
/*
Plugin Name: custom dashboard widget
Plugin URI: http://en.bainternet.info
Description: custom dashboard widget with control form
Version: 0.1
Author: bainternet
Author URI: http://en.bainternet.info
*/

//show widget
function custom_dashboard_widget_coach() {
    //get saved data
    if ( !$widget_options = get_option( 'my_dashboard_widget_options' ) )
        $widget_options = array();
    $saved_team = isset($widget_options['team'])? $widget_options['team'] : '';
        echo "
        <p><strong>Finalized Game</strong></p>
        <div class='team_class_wrap'>
            <label>Class {$saved_team}</label>
        </div>
        ";
}

//configure and update widget
function custom_dashboard_widget_coach_handle(){
    //get saved data
    if ( !$widget_options = get_option( 'my_dashboard_widget_options' ) )
        $widget_options = array();

    //process update
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['my_dashboard_widget_options']) ) {
        //minor validation
            $widget_options['team'] = wp_kses($_POST['my_dashboard_widget_options']['team'],array() );
        //save update
                update_option( 'my_dashboard_widget_options', $widget_options );
        }

    //set defaults  
    if(!isset($widget_options['team']))
        $widget_options['team'] = ''; //you can set the default
        echo "
        <p><strong>Finalize Game</strong></p>
        <div class='team_class_wrap'>
            <label>Class</label>
            <select name='my_dashboard_widget_options[team]' id='team'>
                <option value='5a' ".selected( $widget_options['team'], '5a', false ).">5A</option>
                <option value='4a' ".selected( $widget_options['team'], '4a', false ).">4A</option>
                <option value='3a' ".selected( $widget_options['team'], '3a', false ).">3A</option>
                <option value='2a' ".selected( $widget_options['team'], '2a', false ).">2A</option>
            </select>
        </div>
        ";
}

//register widget
function add_custom_dashboard_widget_coach() {
        wp_add_dashboard_widget('custom_dashboard_widget_coach', 'My Team', 'custom_dashboard_widget_coach', 'custom_dashboard_widget_coach_handle');
}
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget_coach');

Which will give you: enter image description here

And once you click "configure" you get the form:enter image description here

And when you click submit the data would be saved and you will see this:enter image description here

Now in my example the data is stored in the options table as an array in a new row but you can use the dashboard_widget_options option for widget specific data (widget settings) or usermeta for user specific data.

4
  • Hmmmm. This does indeed work for me when I log in as an admin. But it seems you have to configure it every time you want to submit the form, and the configure link isn't available to those who log in on the reduced permissions custom user role I created. Is there a way to save the state of the widget when its already configured or do I need to look into just building a plugin to do what I want with its own admin page?
    – John B
    Commented Jan 2, 2013 at 14:43
  • The only other options is to print and process the from in your custom_dashboard_widget_coach (function that displays the widget).
    – Bainternet
    Commented Jan 2, 2013 at 14:51
  • Hmm. I think between the two of these I should be able to work it out. I will likely end up having to print and process it in my display widget as I want it to only show up for users of certain roles anyway and then load custom content based on their role. Thanks for your help.
    – John B
    Commented Jan 2, 2013 at 16:27
  • For the records, I just added an example to the Codex with what I've learned here. Great stuff! . . I suspect this was the only resource in the web dealing with this.
    – brasofilo
    Commented Oct 20, 2013 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.