如何为我的自定义管理菜单页面添加通知气泡

时间:2018-03-21 作者:wpdev

我创建了自己的联系人表单WP_List_Table 在wp admin中显示提交的表单。我编辑过this 示例WP\\U List\\U表格来自github 而且效果很好。

以下是我添加自定义管理菜单页面的方式:

function contact_form_create() {
    add_menu_page(\'Contact Forms\', \'Contact Forms\', \'administrator\', \'contact-form\', \'contact_form_page_handler\');
}

add_action(\'admin_menu\', \'contact_form_create\');
但现在我想通知你。因此,当访问者提交联系表单时,我的表格会在wp admin中显示通知,如下所示:

2 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

好的,准确地说,它与WP\\u List\\u Table无关。所有您需要做的是添加一些额外的信息在您的自定义管理页面注册。

WordPress使用两个类来显示这些通知气泡:

更新插件等待修改您的通知与插件无关,因此最好使用第二个类。您应该这样更改功能:

function contact_form_create() {
    $notification_count = 2; // <- here you should get correct count of forms submitted since last visit

    add_menu_page(
        \'Contact Forms\',
        $notification_count ? sprintf(\'Contact Forms <span class="awaiting-mod">%d</span>\', $notification_count) : \'Contact Forms\',
        \'administrator\', // <- it would be nicer to use capability in here \'manage_options\' 
        \'contact-form\',
        \'contact_form_page_handler\'
    );
}

add_action(\'admin_menu\', \'contact_form_create\');
所以主要的变化是,如果有一些通知要显示,那么我们添加<span class="awaiting-mod">%d</span> 到该页的标题。

您现在唯一要做的就是获得自上次访问以来提交的表单的正确数量。最简单的方法是将提交表单记录的最后一个ID存储在自定义DB表中,作为一些选项,并根据此ID计算新记录。但很难说如何准确计算它们,因为我不知道您如何存储这些提交表单等,所以我将这部分留给您。

SO网友:revive

这里有一个(大的)片段,我专门用于重力形式,但可以根据需要进行调整,以获取任何地方的计数。

此代码段获取count\\u entries()调用中数组()中所有表单的计数,以便父菜单项显示所有未读条目(来自该数组中的表单ID)

然后在构建菜单时,我更改count\\u entries()调用和URL中的ID-请参见代码段中的注释。

function register_my_custom_menu_page() {
    $search_criteria = array(
        \'status\'     => \'active\', //Active forms 
        \'field_filters\' => array( //which fields to search
            array(
                \'key\' => \'is_read\', \'value\' => false, // let\'s just get the count for entries that we haven\'t read yet.
            )
          )
        );

    // Add the form IDs to the array below, the parent menu will show ALL unread entries for these forms
    $notification_count = GFAPI::count_entries( array(1,4,5,6,11,13), $search_criteria );
    
    add_menu_page(
        \'Full Quote Form submissions\', // Page Title
        // here we\'re going to use the var $notification_count to get ALL the form IDS and their counts... just for the parent menu item
        $notification_count ? sprintf( \'Quotes <span class="awaiting-mod">%d</span>\', $notification_count ) : \'View Quotes\',
        \'manage_options\', // Capabilities 
        \'admin.php?page=gf_entries&id=13\', // menu slug
        \'\', // callback function
        \'dashicons-format-aside\', // icon URL
        6 // position
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'View Full Quotes\', // name of the page
        // The ID on the next line, 13 in this example, matches the form ID in the URL on the LAST line. Notice we\'re NOT using the var $notification_count from above, as that contains the entry count for ALL the forms
        GFAPI::count_entries( 13, $search_criteria ) ? sprintf( \'Full Quotes <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 13, $search_criteria ) ) : \'Full Quotes\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        \'admin.php?page=gf_entries&id=13\'
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'View Short Quotes\', // name of the page
        // The ID on the next line, 1 in this example, matches the form ID in the URL on the LAST line.     
        GFAPI::count_entries( 1, $search_criteria ) ? sprintf( \'Short Quotes <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 1, $search_criteria ) ) : \'Short Quotes\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        \'admin.php?page=gf_entries&id=1\'
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'Sell Your Equipment\', // name of the page
        // The ID on the next line, 5 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 5, $search_criteria ) ? sprintf( \'Selling Equip <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 5, $search_criteria ) ) : \'Selling Equip\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        \'admin.php?page=gf_entries&id=5\'
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'Equipment Wanted\', // name of the page
        // The ID on the next line, 6 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 6, $search_criteria ) ? sprintf( \'Equip Wanted <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 6, $search_criteria ) ) : \'Equip Wanted\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call  
        \'admin.php?page=gf_entries&id=6\'
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'Appraisal Requests\', // name of the page
        // The ID on the next line, 11 in this example, matches the form ID in the URL on the LAST line.                
        GFAPI::count_entries( 11, $search_criteria ) ? sprintf( \'Appraisal Requests <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 11, $search_criteria ) ) : \'Appraisal Requests\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        \'admin.php?page=gf_entries&id=11\'
    );
    add_submenu_page( 
        \'admin.php?page=gf_entries&id=13\', // this needs to match menu slug of the parent
        \'Contact Form\',  // name of the page
        // The ID on the next line, 4 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 4, $search_criteria ) ? sprintf( \'Contact Form <span class="awaiting-mod">%d</span>\', GFAPI::count_entries( 4, $search_criteria ) ) : \'Contact Form\',
        \'manage_options\', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        \'admin.php?page=gf_entries&id=4\'
    );
    
}
add_action( \'admin_menu\', \'register_my_custom_menu_page\' );

结束