Halloween Sale 15% off Ends in See the plans
PHP

Network-Wide Dashboard Announcement

Adds an "Announcement" box to Network Admin > Settings; whatever a super admin writes there appears as a dismissible notice on every site's dashboard.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

add_action( 'wpmu_options', function () {
    ?>
    <h2><?php esc_html_e( 'Network announcement' ); ?></h2>
    <table class="form-table" role="presentation">
        <tr>
            <th scope="row"><label for="wpide_network_announcement"><?php esc_html_e( 'Announcement' ); ?></label></th>
            <td>
                <textarea name="wpide_network_announcement" id="wpide_network_announcement" rows="3" class="large-text"><?php echo esc_textarea( get_site_option( 'wpide_network_announcement', '' ) ); ?></textarea>
                <p class="description"><?php esc_html_e( 'Shown on the dashboard of every site to users who can edit posts.' ); ?></p>
            </td>
        </tr>
    </table>
    <?php
} );

// Network Settings already verifies its own nonce and capability before this runs.
add_action( 'update_wpmu_options', function () {
    if ( isset( $_POST['wpide_network_announcement'] ) && current_user_can( 'manage_network_options' ) ) {
        update_site_option( 'wpide_network_announcement', wp_kses_post( wp_unslash( $_POST['wpide_network_announcement'] ) ) );
    }
} );

add_action( 'admin_notices', function () {
    $message = get_site_option( 'wpide_network_announcement', '' );
    $screen  = get_current_screen();

    if ( '' === trim( $message ) || ! current_user_can( 'edit_posts' ) || ! $screen || 'dashboard' !== $screen->id ) {
        return;
    }

    echo '<div class="notice notice-info is-dismissible"><p>' . wp_kses_post( $message ) . '</p></div>';
} );

Notes

Basic HTML (links, bold) is allowed. Clear the field to remove the notice.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.