如何在通过unctions.php或plugin创建Widget时使用CONTROL_CALLBACK?

时间:2012-12-31 作者:John B

我在函数中创建了一个仪表板小部件。php文件。我想让它能够在提交表格后更新数据库中的一些内容。根据codex文档,我应该使用wp\\u add\\u dashboard\\u widget()的第四个参数$control\\u callback来处理我提交的帖子。

因此,我创建了处理程序函数,并将其包含在函数中。php文件。当我提交表格时,什么都没有发生。我想了一会儿,然后用add\\u action()将回调函数连接到wp\\u dashboard\\u setup;我的回调函数现在运行,但无论我是否将control\\u回调函数传递给wp\\u add\\u dashboard\\u widget(),它都会运行,这让我觉得我的解决方案有点像黑客。

我做错了吗?还有更好的方法吗?还有,我的表单应该提交到任何地方吗?或者它可以直接提交到它绘制的地方吗?

这是我的完整代码:

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 个回复
最合适的回答,由SO网友:Bainternet 整理而成

实际上,“发件人”字段只应位于control_callback 函数,包括形式和处理,请尝试以下操作:

<?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\');
这将为您提供:enter image description here

单击“配置”后,您将获得以下表单:enter image description here

当您单击“提交”时,数据将被保存,您将看到:enter image description here

现在,在我的示例中,数据作为新行中的数组存储在选项表中,但您可以使用dashboard_widget_options 小部件特定数据选项(小部件设置)或usermeta 用于用户特定数据。

结束

相关推荐

Remove Widgets in Dashboard

另一个noob问题。。。当作者登录并访问其仪表板时,他们会看到各种小部件,例如Internet Explorer警告、WordPress动态新闻。是否要为所有用户(现在和将来)删除所有这些内容,以便他们只看到“快速按”&;\'现在的小部件?谢谢