简体   繁体   中英

Wordpress;how to remove_action from a Plugin to hide admin_notices created by another Plugin

I am a beginner to WP hooks, (actions and filters) I was able to create a Plugin that shows all 4 admin notices in WP dashboard

here the Plugin-one code

    /* Start admin notices on */
  
function display_admin_notice() {
    ?>
    <div class ="notice notice-success is-dismissible"><p>Congratulatios! it seems you have a nice success notice</p></div>
    <div class = "notice notice-error"><p>What a nice error message</p> </div>
    <div class = "notice notice-warning"><p>WARNING!!! site about to blow up in pieces</p></div>
    <div class = "notice notice-info is-dismissible"><p>INFO - you must renew license</p></div>

    <?php
  }
  add_action( 'admin_notices', 'display_admin_notice' );
  
/* Stop admin notices on */

it works perfectly.

Now I have another plugin that hides ALL notifications

/* Start Block ALL admin notices */

add_action('admin_enqueue_scripts', 'block_dismissable_admin_notices');
add_action('login_enqueue_scripts', 'block_dismissable_admin_notices');

function block_dismissable_admin_notices() {
   echo '<style>.wp-core-ui .notice{ display: none !important; }</style>';
}

/* Stop Block ALL admin notices */

it works fine too

BUT I need to REMOVE the action from the first Plugin, NOT to hide all notifications, I mean, is there a way to get the

add_action( 'admin_notices', 'display_admin_notice'

from the first Plugin and remove_action so the Plugin hides/block ONLY the admin_notices from THAT plugin and NOT from all the Wordpress core or another plugins?

Thanks in advance

Instead of loading code and then unloading it (pointless) you could add an if statement to conditionally check if the action hook is needed.

eg.

$noticesOn = true; // Conditional setting could be here

if ( $noticesOn == true ) {
    add_action('admin_enqueue_scripts', 'block_dismissable_admin_notices');
    add_action('login_enqueue_scripts', 'block_dismissable_admin_notices');
}

ok, I finally got it

this is the solution

add_action( 'init', 'remove_my_action' );
function remove_my_action()
{
global $wp_filter;
remove_action( 'admin_notices', 'display_admin_notice');
}

it works perfect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM