Trying to call "admin_notices" hook from a static function. Getting no output. While normal calling to the same hook is working.
use Awraq\Init;
use Awraq\Notice;
/**
* Initialize the activation works of the plugin.
*/
function awraq_activate_plugin()
{
/* NOT WORKING */
Notice::error('Activation failed');
/* WORKING */
//add_action('admin_notices', 'simplefunction');
}
register_activation_hook(__FILE__, 'awraq_activate_plugin');
function simplefunction()
{
echo '<div class="notice notice-error is-dismissible">
<p>Activation failed</p></div>';
}
class
namespace Awraq;
if (!defined('ABSPATH')) exit;
class Notice
{
private static $class = '';
private static $msg = '';
public static function error($msg = 'error', $is_dismissible = TRUE)
{
self::$class = ($is_dismissible == TRUE) ? 'notice notice-error is-dismissible' : 'notice notice-error';
self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
add_action('admin_notices', array(__CLASS__, 'awraq_notice'));
}
public static function warning($msg = 'warning', $is_dismissible = TRUE)
{
self::$class = ($is_dismissible == TRUE) ? 'notice notice-warning is-dismissible' : 'notice notice-warning';
self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
add_action('admin_notices', array(__CLASS__, 'awraq_notice'));
}
public static function success($msg = 'success', $is_dismissible = TRUE)
{
self::$class = ($is_dismissible == TRUE) ? 'notice notice-success is-dismissible' : 'notice notice-success';
self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
add_action('admin_notices', array(__CLASS__, 'awraq_notice'));
}
public static function info($msg = 'info', $is_dismissible = TRUE)
{
self::$class = ($is_dismissible == TRUE) ? 'notice notice-info is-dismissible' : 'notice notice-info';
self::$msg = __($msg, AWRAQ_TEXT_DOMAIN);
add_action('admin_notices', array(__CLASS__, 'awraq_notice'));
}
public static function awraq_notice()
{
$class = self::$class;
$msg = self::$msg;
if ($class == '' and $msg == '') {
return;
}
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($msg));
}
}
add_action('admin_notices', array(CLASS, 'awraq_notice'))
is not the same thing as callingself::awraq_notice()
. Usecall_user_func( array( __CLASS__, 'awraq_notice' );
and you'll get the same result. This approach isn't going to work.