Move WordPress Admin Notifications and Clean your Dashboard with a single script

This code helps you declutter your WordPress dashboard by moving all admin notifications to a dedicated “Notifications” submenu under the Dashboard menu. It also includes a dynamic badge that shows the total number of pending notifications, ensuring you never miss important updates.

Simply add this code using the WP Code Snippet plugin, and your dashboard will remain clean and distraction-free. All notifications will still be accessible in one organized place, making your admin experience more streamlined.

// Capture admin notices and store them
add_action('admin_notices', 'acn_capture_admin_notices', 1);
add_action('all_admin_notices', 'acn_capture_admin_notices', 1);

function acn_capture_admin_notices() {
    global $acn_stored_notices;

    // Initialize the stored notices array if not already set
    if (!isset($acn_stored_notices)) {
        $acn_stored_notices = [];
    }

    // Capture the notice output and store it
    ob_start();
    do_action('default_admin_notices');
    $notices = ob_get_clean();

    if (!empty($notices)) {
        $acn_stored_notices[] = $notices;
    }
}

// Create a sub-menu under the Dashboard menu for notifications
add_action('admin_menu', 'acn_create_dashboard_notifications_submenu');
function acn_create_dashboard_notifications_submenu() {
    add_submenu_page(
        'index.php',
        'Notifications',
        'Notifications' . acn_get_notification_count_badge(),
        'manage_options',
        'dashboard-clean-notifications',
        'acn_display_notifications'
    );
}

// Add a badge for notification count
function acn_get_notification_count_badge() {
    global $acn_stored_notices;

    // Count the number of stored notices
    $count = isset($acn_stored_notices) ? count($acn_stored_notices) : 0;

    // Only display the badge if there are notifications
    if ($count > 0) {
        return " <span class='awaiting-mod updates-count'>$count</span>";
    }

    return '';
}

// Display notifications on the Notifications page
function acn_display_notifications() {
    global $acn_stored_notices;

    echo '<div class="wrap">';
    echo '<h1>Dashboard Notifications</h1>';

    if (!empty($acn_stored_notices)) {
        foreach ($acn_stored_notices as $notice) {
            echo $notice;
        }
    } else {
        echo '<p>No notifications available.</p>';
    }

    echo '</div>';
}

// Suppress default notices on all other admin pages
add_action('in_admin_header', 'acn_suppress_admin_notices', 1);
function acn_suppress_admin_notices() {
    $screen = get_current_screen();
    if ($screen->id !== 'dashboard_page_dashboard-clean-notifications') {
        remove_all_actions('admin_notices');
        remove_all_actions('all_admin_notices');
    }
}

// Update the badge count dynamically
add_action('admin_menu', 'acn_update_notification_badge', 99);
function acn_update_notification_badge() {
    global $submenu;

    // Ensure the submenu under "Dashboard" exists
    if (isset($submenu['index.php'])) {
        foreach ($submenu['index.php'] as &$menu_item) {
            // Look for the 'Notifications' submenu
            if ($menu_item[2] === 'dashboard-clean-notifications') {
                $menu_item[0] = 'Notifications' . acn_get_notification_count_badge();
                break;
            }
        }
    }
}

Leave a Comment

We are just this form away!!!

Fill the details and we will reach out to you shortly. 

Please enable JavaScript in your browser to complete this form.