Add Snippet To Project
As a WordPress administrator, staying up-to-date with the latest updates is crucial to maintaining a secure and functional website. Fortunately, WordPress makes it easy to see when updates are available for your plugins, themes, and core software. In this article, we’ll show you how to display update notifications specifically for admins so you can stay informed and keep your site running smoothly.
function wpturbo_display_update_notification() {
if ( current_user_can( 'manage_options' ) ) {
echo '<div class="notice notice-success is-dismissible"><p>New updates available. <a href="' . admin_url( 'update-core.php' ) . '">Update Now</a></p></div>';
}
}
add_action( 'admin_notices', 'wpturbo_display_update_notification' );
The wpturbo_display_update_notification()
function checks if the currently logged-in user has the capability to manage options. If the user has this capability (i.e., is an administrator), then the function creates and displays a success notice on the WordPress admin dashboard, notifying the user of available updates. If the user does not have this capability, then the function does nothing.
The HTML markup for the success notice is enclosed in single quotes and consists of a div
element with the classes notice
, notice-success
, and is-dismissible
. The is-dismissible
class adds a close button to the notice, allowing the user to dismiss it if desired. The text of the notice contains a message informing the user that updates are available and providing a link to the WordPress updates page. Here’s the code snippet:
if ( current_user_can( 'manage_options' ) ) {
echo '<div class="notice notice-success is-dismissible"><p>New updates available. <a href="' . admin_url( 'update-core.php' ) . '">Update Now</a></p></div>';
}
The admin_url( 'update-core.php' )
function generates the URL for the WordPress updates page. When the user clicks on the link, they will be taken to this page where they can proceed with updating their website.
Finally, the wpturbo_display_update_notification()
function is hooked into the admin_notices
action using the add_action
function. This ensures that the update notification is displayed on the WordPress admin dashboard whenever WordPress renders its admin notices but only to users with the manage_options
capability, which is usually just administrators.