4

I created my own contact form and I'm using WP_List_Table to display submitted forms in wp-admin. I edited this example WP_List_Table from github and it works fine.

This is how I add my custom admin menu page:

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');

But now I want to make a notification. So when visitor submit a contact form, then my table shows me a notification in wp-admin like this:

2
  • Well, it has nothing to do with WP_List_Table, I guess. Could you show us the part of your code which is responsible for adding menu_page or menu_subpage in wp-admin? – Krzysiek Dróżdż Mar 21 '18 at 15:56
  • Edited post. I used add_menu_page and args. – bilimokur Mar 21 '18 at 16:09
7

OK, so it has nothing to do with WP_List_Table, to be precise. All you need to do is to add some additional info during registration of your custom admin page.

There are two classes used by WordPress to display these notification bubbles:

  • update-plugins
  • awaiting-mod

Your notifications have nothing to do with plugins, so it will be nicer to use the second class. You should change your function like this:

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');

So the main change is that if there are some notifications to show, then we add <span class="awaiting-mod">%d</span> to title of that page.

The only thing you'll have to do now is to get correct number of form submits since last visit. The easiest way to do this would be store last ID of submited form record in your custom DB table as some option and counting new records based on this ID. But it's hard to say exactly how to count them, since I have no info about how you store this submitted forms and so on, so I leave this part to you.

2
  • Thank you Krzysiek. I count db tables and it works good. Is it possible to remove notification if open contact form page? – bilimokur Mar 21 '18 at 16:53
  • @bilimokur the notification bubble will hide automatically, if you'll set the notifications count to 0. So you'll have to find some way to set this count to 0 every time that page is opened. – Krzysiek Dróżdż Mar 21 '18 at 16:56
0

Here is a (large) snippet I use for Gravity Forms specifically, but can be adapted to get the count from wherever you need it.

This snippet gets the count for all the forms in the array() within the count_entries() call so the parent menu items shows ALL unread entries (from the form IDs in that array)

Then as I build the menu, I change the ID in both the count_entries() call AND the URL - see comments in the snippet.

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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