Disable Plugin Notices WordPress
January 17, 2025 · AI Conversations
### 1. **Using a Code Snippet**
You can add a custom code snippet to your theme's `functions.php` file or in a custom plugin. Here's how:
#### Example Code:
add_action('admin_notices', function () {
global $pagenow;
// List of plugin notices to hide
$plugins_to_hide = [
'plugin-slug-1/plugin-file.php', // Replace with the plugin's main file path
'plugin-slug-2/plugin-file.php'
];
foreach ($plugins_to_hide as $plugin) {
if (is_plugin_active($plugin)) {
remove_all_actions('admin_notices');
remove_all_actions('network_admin_notices');
}
}
});
### 2. **Target Specific Notice Markup with CSS**
If the plugin's notices have unique classes or IDs, you can hide them using CSS.
#### Example Code:
add_action('admin_head', function () {
echo '<style>
.plugin-slug-notice-class { display: none !important; } /* Replace with the notice's unique class */
#specific-notice-id { display: none !important; } /* Replace with the notice's unique ID */
</style>';
});
### 3. **Using a Plugin to Manage Notices**
If you'd prefer not to modify code directly, you can use a plugin like [Admin Notices Manager](https://wordpress.org/plugins/admin-notices-manager/) to control notices.
### 4. **Modify the Plugin Code (Not Recommended)**
You can edit the specific plugin's code to disable its admin notice functions. However, this method is **not recommended** because:
– Changes will be lost when the plugin is updated.
– It may break functionality or violate the plugin's terms of use.
Instead, try to override its notice functions using hooks if available.
—
If you let me know the specific plugins you want to target or share the notice details, I can provide a more tailored solution.
### Suppress Notices in Your Plugin
Add the following code to your custom plugin:
add_action('admin_init', function () {
// Remove other plugin admin notices
remove_all_actions('admin_notices');
remove_all_actions('network_admin_notices');
// Optional: Add back your custom admin notices
add_action('admin_notices', 'your_custom_admin_notice');
});
// Example of adding your own notice
function your_custom_admin_notice() {
echo '<div class="notice notice-success is-dismissible">
<p>Your custom plugin is running!</p>
</div>';
}
### Explanation
1. **`remove_all_actions('admin_notices');`**
This removes all admin notices hooked to the `admin_notices` action, including those from other plugins.
2. **Optional Custom Notices:**
After clearing all notices, you can add your own notices using the `admin_notices` action.
—
### Allow Notices from Specific Plugins
If you want to suppress all notices except those from specific plugins, you can use the following approach:
add_action('admin_notices', function () {
// Output buffer to capture notices
ob_start();
});
add_action('all_admin_notices', function () {
// Get the captured notices
$notices = ob_get_clean();
// Whitelist of plugin classes or unique notice content to allow
$whitelisted_plugins = [
'unique-class-from-plugin-1',
'unique-id-from-plugin-2'
];
// Display only notices from whitelisted plugins
foreach ($whitelisted_plugins as $identifier) {
if (strpos($notices, $identifier) !== false) {
echo $notices;
break;
}
}
});
### Explanation
– **Capture Notices:** The `ob_start()` function captures all output in the `admin_notices` hook.
– **Filter Notices:** Use the `strpos()` function or similar logic to check if a specific plugin's notice matches a unique class, ID, or string in the notice HTML.
– **Output Notices:** Only display notices that match your whitelist.
—
Let me know if you need help identifying specific plugin notice classes or IDs!
### Code to Add in Your Custom Plugin
add_action('admin_head', function () {
// Add custom CSS to hide all admin notices
echo '<style>
.notice, .update-nag, .error, .updated, .is-dismissible {
display: none !important;
}
</style>';
});
### Explanation
– This approach uses CSS to hide all notices (`.notice`, `.update-nag`, etc.) on the admin pages.
– You can also scope this CSS to specific admin pages by using `get_current_screen()`.
—
### More Refined Approach: Remove Actions Dynamically
If you only want to remove specific admin notices programmatically, you can intercept and clear them:
add_action('admin_init', function () {
// Remove all admin notices from other plugins
remove_all_actions('admin_notices');
remove_all_actions('network_admin_notices');
// Optional: Add back your custom admin notices
add_action('admin_notices', 'your_custom_admin_notice');
});
function your_custom_admin_notice() {
echo '<div class="notice notice-info is-dismissible">
<p>Custom admin notice from your plugin.</p>
</div>';
}
—
### Advanced: Hide Notices for Specific Plugins
If you want to disable notices from specific plugins while keeping others visible, you can capture and filter the output of the notices:
add_action('admin_notices', function () {
// Start output buffering to capture notices
ob_start();
}, 0);
add_action('all_admin_notices', function () {
// Get the captured notices
$notices = ob_get_clean();
// List of keywords or classes to identify notices to block
$blocked_plugins = [
'plugin-unique-class', // Replace with a unique class from the plugin notice
'specific-notice-id', // Replace with the notice ID
];
// Check if the notice matches any blocked plugin identifier
foreach ($blocked_plugins as $identifier) {
if (strpos($notices, $identifier) !== false) {
// Skip displaying this notice
return;
}
}
// Output allowed notices
echo $notices;
}, PHP_INT_MAX);
### How It Works
1. **Capture Notices:** Use `ob_start()` to capture the output of all admin notices.
2. **Filter Notices:** Search for specific identifiers (class names, IDs, or text) in the captured output.
3. **Block Notices:** If a match is found, block the notice; otherwise, display it.
—
Let me know if you need help identifying the specific classes or IDs of notices from other plugins.
1. Preventing all notices from being rendered.
2. Specifically disabling direct output from `echo` or `print`.
Here's how you can achieve this:
—
### Code to Add in Your Custom Plugin
#### 1. **Intercept All Notices**
Hook into the `admin_notices` and `network_admin_notices` actions and block their output, regardless of the source.
add_action('admin_init', function () {
// Suppress all admin notices
remove_all_actions('admin_notices');
remove_all_actions('network_admin_notices');
// Optionally add your custom notices here if needed
add_action('admin_notices', function () {
// Example of adding a controlled custom notice
echo '<div class="notice notice-success is-dismissible">
<p>Your custom plugin is running smoothly.</p>
</div>';
});
});
—
#### 2. **Use Output Buffering to Block All Directly Printed Notices**
Capture any unexpected output from plugins (including your own) and suppress it.
add_action('admin_head', function () {
// Start output buffering
ob_start();
}, 0);
add_action('shutdown', function () {
// Clear output buffering before rendering the admin page
ob_clean();
}, PHP_INT_MAX);
—
#### 3. **Disable Notices Specifically in Your Plugin**
To prevent your custom plugin from unintentionally printing or echoing notices, wrap any output in checks. For instance:
if (defined('HIDE_ADMIN_NOTICES') && HIDE_ADMIN_NOTICES) {
// Avoid printing anything
return;
}
// Example of a controlled output
echo '<div class="notice notice-info">
<p>This is a controlled notice from your plugin.</p>
</div>';
To enable this behavior dynamically:
– Define `HIDE_ADMIN_NOTICES` as `true` where appropriate.
—
#### 4. **Hide Notices Using CSS**
Additionally, as a fallback, you can use CSS to hide any notices that might slip through.
add_action('admin_head', function () {
echo '<style>
.notice, .update-nag, .error, .updated, .is-dismissible {
display: none !important;
}
</style>';
});
—
### Combined Implementation
Here's a full implementation that combines all the above methods:
// Suppress all admin notices
add_action('admin_init', function () {
remove_all_actions('admin_notices');
remove_all_actions('network_admin_notices');
});
// Block direct output
add_action('admin_head', function () {
ob_start();
}, 0);
add_action('shutdown', function () {
ob_clean();
}, PHP_INT_MAX);
// Optional: Add controlled custom notice
add_action('admin_notices', function () {
if (!defined('HIDE_ADMIN_NOTICES') || !HIDE_ADMIN_NOTICES) {
echo '<div class="notice notice-info is-dismissible">
<p>Controlled notice from your custom plugin.</p>
</div>';
}
});
// Fallback: Hide notices using CSS
add_action('admin_head', function () {
echo '<style>
.notice, .update-nag, .error, .updated, .is-dismissible {
display: none !important;
}
</style>';
});
—
### Key Points
1. **Dynamic Control:** Use constants like `HIDE_ADMIN_NOTICES` to control whether notices are hidden.
2. **Output Buffering:** Prevent any direct `echo` or `print` from rendering unexpected notices.
3. **CSS Fallback:** Ensure any missed notices are visually hidden.
This approach guarantees a clean admin interface where no notices, including those printed from your plugin, appear.
<script>
window.location.href='admin.php?page=generate-label&orderids=" . implode(',', $order_ids) . "'
</script>
";
exit;
}
function pmm_generate_label_style()
{
?>
<style>
div#custom-popup {
margin-top: 50px;
}
div#generate_label {
background: #fff;
}
.generate_label {
width: 50%;
text-align: center;
}
.dosage_ins {
margin-top: -3%;
}
.dosage_ins p {
font-weight: 600;
}
.client_name-date {
margin-top: -3%;
}
.warning.url.address td {
margin-top: -15px;
}
.generate_label {
/* height: 55vh; */
}
.order_content {
margin: 2px;
}
.generate_label #product-name {
padding-bottom: 10px;
}
.generate_label #product-name h2 {
margin: 0;
line-height: 1;
font-size: 1.5rem;
}
.generate_label .dosage_ins {
margin-top: 0;
}
.generate_label .dosage_ins td {
font-size: 16px;
margin: 0;
}
p.counselling-point {
font-size: 13px;
margin-top: 0px;
margin-bottom: 0px;
}
.generate_label .client_name-date {
MARGIN: 0;
}
.generate_label .client_name-date h2 {
margin: 0;
padding-top: 6px;
font-size: 20px;
}
.generate_label .warning.url.address {
line-height: 5px;
padding-top: 8px;
}
.generate_label .warning.url.address td {
margin: 0;
font-size: 14px;
font-weight: 500;
padding-top: 4px;
}
.generate_label .warning.url.address td i {
font-weight: bold;
font-style: normal;
}
</style>
<script>
jQuery(document).ready(function() {
var printWindow;
jQuery('.printButton').click(function() {
if (!printWindow || printWindow.closed) {
function adjustFontSize(selector, threshold, defaultSize) {
jQuery(selector).each(function(index, element) {
var content = jQuery(element).text();
var contentLength = content.length;
var fontSize = '';
if (contentLength < 50) {
fontSize = '7px';
} else if (contentLength >= 30 && contentLength <= 200) {
fontSize = '7px';
} else {
fontSize = '7px';
}
jQuery(element).css('font-size', fontSize || defaultSize);
});
}
var productname = jQuery('.product_name').html();
var dosageIns = jQuery('.dosage_ins').html();
var counsellingPoints = jQuery('.counselling_points').html();
var order_content = jQuery('.order-data').html();
var prod_name = adjustFontSize('.product_name', 100, '8px');
var dosage_ins = adjustFontSize('.dosage_ins', 200, '8px');
var counselling_points = adjustFontSize('.counselling_points', 200, '8px');
var contentToPrint = jQuery('.print-data').html();
printWindow = window.open('', '', 'width=600, height=600');
printWindow.document.write('<html><head><title>Order Details</title>');
printWindow.document.write('<div class="chatgpt-code-wrap overflow-auto mb-2"><pre class="m-0"><code class="bg-light p-2 d-block rounded"><style>
@media print{@import url("https://fonts.cdnfonts.com/css/liberation-sans");body{font-family:"Liberation Sans"} @page {size: 7cm 3.7cm; margin: 0; font-size:; }.order-data{page-break-inside:avoid}.generate_label .product_name h2{color:#000;padding-top:0px;padding-bottom:0px;margin-bottom:0px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;font-size: ' + prod_name + ';}div#custom-popup{margin-top:5px}div#generate_label{background:#fff}.generate_label{text-align:center; border:none !important; }.client_name-date,.dosage_ins td{ font-weight:600;padding-top:0px;margin-top:0px;}.warning.url.address td{margin-top:0px}.order_content{margin:0px !important}.generate_label #product-name{padding-bottom:4px}.generate_label .dosage_ins td{font-size:' + dosage_ins + ';margin:0} td.counselling-point{font-size:' + counselling_points + ';margin-top:0;margin-bottom:0}.generate_label .client_name-date{margin:0}.generate_label .client_name-date td h2{margin:0;padding-top:0px;font-size:7px}.generate_label .warning.url.address td{line-height:1.2;padding-top:0}.generate_label .warning.url.address td{margin:0;font-size:6px;font-weight:500;padding-top:0}.generate_label .warning.url.address td i{font-weight:700;font-style:normal}@media print { .page-break { page-break-before: always; } }}
</style></code></pre></div></head><body>');
printWindow.document.write(contentToPrint);
printWindow.document.close();
printWindow.print();
} else {
printWindow.focus();
}
});
});
</script>
### Steps to Hide Print Content for Specific Roles:
1. **Restrict the Print Button Visibility**
Use conditional logic to check the user role and hide the "Print Order Data" button if the user does not have the required capability.
2. **Prevent Printing via Media Query**
Add a media query for the `@media print` rule to hide the content if the page is accessed through the print function.
3. **Update the Plugin Code**
Modify the `generate_labels_code` function to include the checks and ensure secure printing.
Here’s an updated snippet of the plugin:
“`php
function generate_labels_code()
{
if (!current_user_can('print_label')) {
wp_die(__('You do not have permission to access this page.', 'woocommerce'));
}
$url = site_url();
echo '';
echo ' Back to Orders
';
echo '';
$order_ids = explode(",", sanitize_text_field($_GET['orderids']));
foreach ($order_ids as $order_id) {
$order = wc_get_order($order_id);
if (!$order) {
continue;
}
$order_data = $order->get_data();
$first_name = ucfirst(strtolower($order_data['billing']['first_name']));
$last_name = ucfirst(strtolower($order_data['billing']['last_name']));
$order_items = $order->get_items();
foreach ($order_items as $item_id => $item) {
$productID = $item->get_product_id();
$product_name = get_the_title($productID);
$variation_id = $item->get_variation_id();
$dosage_instruction = get_post_meta($variation_id, '_textarea', true);
$counselling_products = get_post_meta($productID, 'counselling', true);
$couse_label = get_post_meta($variation_id, '_couselling_label', true);
$prod_label = get_post_meta($variation_id, '_product_label', true);
$qty_label = get_post_meta($variation_id, '_quantity_label', true);
$qqty = explode('|', $qty_label);
foreach ($qqty as $qq) {
echo '
### Add the CSS Rule to Hide Admin Notices
In the `pmm_generate_label_style` function, include a print-specific CSS rule that hides admin notices (`.notice` class) when printing:
function pmm_generate_label_style()
{
?>
<style>
/* Existing styles */
/* Add this print-specific style to hide admin notices */
@media print {
.notice,
.updated,
.error,
.is-dismissible {
display: none !important;
}
}
</style>
<?php
}
add_action('admin_head', 'pmm_generate_label_style');
### Explanation:
1. **CSS Rules**:
– `.notice`: General admin notice class.
– `.updated`: For updated messages.
– `.error`: For error messages.
– `.is-dismissible`: For notices with dismissible functionality.
2. **`@media print`**:
This ensures the styles apply only during printing.
3. **`display: none !important;`**:
Hides the targeted elements forcefully, even if other styles attempt to override it.
Now, when the "Print" functionality is triggered, admin notices will not appear in the printed content. Let me know if you need further customization!