3

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:

  • 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
5

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.

  • 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

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.