| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Hide WP Admin Notifications |
| 4 |
* Description: Hides all WordPress admin dashboard notifications with an option to show them temporarily. Visit the settings page to temporarily show the notices. |
| 5 |
* Version: 0.2.2 |
| 6 |
* Author: Yoda Of WP |
| 7 |
* Requires at least: 5.2 |
| 8 |
* Tested up to: 6.9 |
| 9 |
* Requires PHP: 7.0 |
| 10 |
* License: GPLv2 or later |
| 11 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 12 |
*/ |
| 13 |
|
| 14 |
// Prevent direct access |
| 15 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 16 |
|
| 17 |
define( 'HWAN_PLUGIN_VERSION', '0.2.2' ); |
| 18 |
|
| 19 |
// Ensure option exists for seamless version updates |
| 20 |
function hwan_ensure_option_exists() { |
| 21 |
if ( false === get_option( 'hwan_show_dashboard_notices' ) ) { |
| 22 |
add_option( 'hwan_show_dashboard_notices', 'no' ); |
| 23 |
} |
| 24 |
} |
| 25 |
register_activation_hook( __FILE__, 'hwan_ensure_option_exists' ); |
| 26 |
|
| 27 |
// Maintain existing setting during updates |
| 28 |
function hwan_update_check() { |
| 29 |
if ( get_option( 'hwan_plugin_version' ) !== HWAN_PLUGIN_VERSION ) { |
| 30 |
update_option( 'hwan_plugin_version', HWAN_PLUGIN_VERSION ); |
| 31 |
hwan_ensure_option_exists(); |
| 32 |
} |
| 33 |
} |
| 34 |
add_action( 'plugins_loaded', 'hwan_update_check' ); |
| 35 |
|
| 36 |
// Remove admin notices based on the setting |
| 37 |
function hwan_remove_admin_notices() { |
| 38 |
$show_notices = get_option( 'hwan_show_dashboard_notices', 'no' ); |
| 39 |
|
| 40 |
if ( 'yes' !== $show_notices ) { |
| 41 |
global $wp_filter; |
| 42 |
|
| 43 |
// Remove user admin notices |
| 44 |
if ( is_user_admin() && isset( $wp_filter['user_admin_notices'] ) ) { |
| 45 |
unset( $wp_filter['user_admin_notices'] ); |
| 46 |
} |
| 47 |
|
| 48 |
// Remove admin notices |
| 49 |
if ( isset( $wp_filter['admin_notices'] ) ) { |
| 50 |
unset( $wp_filter['admin_notices'] ); |
| 51 |
} |
| 52 |
|
| 53 |
// Remove all admin notices |
| 54 |
if ( isset( $wp_filter['all_admin_notices'] ) ) { |
| 55 |
unset( $wp_filter['all_admin_notices'] ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
add_action( 'admin_print_scripts', 'hwan_remove_admin_notices' ); |
| 60 |
|
| 61 |
// Add plugin settings page |
| 62 |
function hwan_admin_menu() { |
| 63 |
add_options_page( |
| 64 |
'Hide WP Admin Notifications', |
| 65 |
'Hide WP Admin Notifications', |
| 66 |
'manage_options', |
| 67 |
'hide-wp-admin-notifications', |
| 68 |
'hwan_options_page' |
| 69 |
); |
| 70 |
} |
| 71 |
add_action( 'admin_menu', 'hwan_admin_menu' ); |
| 72 |
|
| 73 |
// Register settings and fields |
| 74 |
function hwan_settings_init() { |
| 75 |
register_setting( 'hwan_settings', 'hwan_show_dashboard_notices', 'hwan_sanitize_checkbox' ); |
| 76 |
|
| 77 |
add_settings_section( |
| 78 |
'hwan_settings_section', |
| 79 |
'', |
| 80 |
null, |
| 81 |
'hwan_settings' |
| 82 |
); |
| 83 |
|
| 84 |
add_settings_field( |
| 85 |
'hwan_show_dashboard_notices', |
| 86 |
'Show Dashboard Notices', |
| 87 |
'hwan_show_dashboard_notices_cb', |
| 88 |
'hwan_settings', |
| 89 |
'hwan_settings_section' |
| 90 |
); |
| 91 |
} |
| 92 |
add_action( 'admin_init', 'hwan_settings_init' ); |
| 93 |
|
| 94 |
// Sanitize checkbox value |
| 95 |
function hwan_sanitize_checkbox( $value ) { |
| 96 |
return ( 'yes' === $value ) ? 'yes' : 'no'; |
| 97 |
} |
| 98 |
|
| 99 |
// Show dashboard notices setting field |
| 100 |
function hwan_show_dashboard_notices_cb() { |
| 101 |
$show_notices = get_option( 'hwan_show_dashboard_notices', 'no' ); |
| 102 |
?> |
| 103 |
<label> |
| 104 |
<input type="checkbox" name="hwan_show_dashboard_notices" value="yes" <?php checked( $show_notices, 'yes' ); ?> /> |
| 105 |
<span class="checkmark"></span> |
| 106 |
<span>Check this box to temporarily show all dashboard notices.</span> |
| 107 |
</label> |
| 108 |
<?php |
| 109 |
} |
| 110 |
|
| 111 |
// Display the plugin settings page |
| 112 |
function hwan_options_page() { |
| 113 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 114 |
wp_die( |
| 115 |
esc_html__( |
| 116 |
'You do not have sufficient permissions to access this page.', |
| 117 |
'hide-wp-admin-notifications' |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
?> |
| 122 |
<div class="hwan-settings-container"> |
| 123 |
<h1>Hide WP Admin Notifications</h1> |
| 124 |
<div class="hwan-message"> |
| 125 |
<p>Periodically displaying dashboard notices is essential, as they may provide vital information regarding your site’s security, updates, and other significant announcements.</p> |
| 126 |
</div> |
| 127 |
<form method="post" action="options.php"> |
| 128 |
<?php |
| 129 |
settings_fields( 'hwan_settings' ); |
| 130 |
do_settings_sections( 'hwan_settings' ); |
| 131 |
submit_button(); |
| 132 |
?> |
| 133 |
</form> |
| 134 |
</div> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
// Add settings link to the plugin action links |
| 139 |
function hwan_add_settings_link( $links ) { |
| 140 |
$settings_link = sprintf( |
| 141 |
'<a href="%s">%s</a>', |
| 142 |
esc_url( admin_url( 'options-general.php?page=hide-wp-admin-notifications' ) ), |
| 143 |
esc_html__( 'Settings', 'hide-wp-admin-notifications' ) |
| 144 |
); |
| 145 |
|
| 146 |
array_unshift( $links, $settings_link ); |
| 147 |
|
| 148 |
return $links; |
| 149 |
} |
| 150 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'hwan_add_settings_link' ); |
| 151 |
|
| 152 |
// Enqueue admin styles for the plugin settings page |
| 153 |
function hwan_enqueue_admin_styles( $hook ) { |
| 154 |
if ( 'settings_page_hide-wp-admin-notifications' !== $hook ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
wp_enqueue_style( |
| 159 |
'hwan-admin-styles', |
| 160 |
plugins_url( 'admin.css', __FILE__ ), |
| 161 |
array(), |
| 162 |
HWAN_PLUGIN_VERSION |
| 163 |
); |
| 164 |
} |
| 165 |
add_action( 'admin_enqueue_scripts', 'hwan_enqueue_admin_styles' ); |
| 166 |
|