0

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));
    }
}
4
  • Your class is static but you're setting properties on it that you want to persist when a static method is called later. That's not how static classes or methods work. Jan 5, 2022 at 8:54
  • Hi, there are no issue regarding accessing properties or methods in my context. Just tested. Removed the first occurrence of add_action('admin_notices', array(CLASS, 'awraq_notice')); in "error" method and then calling the self::awraq_notice(). Getting the desired output. I'm suspecting, it might related to timing related thing. Thanks Jan 5, 2022 at 9:34
  • add_action('admin_notices', array(CLASS, 'awraq_notice')) is not the same thing as calling self::awraq_notice(). Use call_user_func( array( __CLASS__, 'awraq_notice' ); and you'll get the same result. This approach isn't going to work. Jan 5, 2022 at 10:07
  • 1
    @PijushGupta see this if for example you wish to display a notice right after your plugin is activated..
    – Sally CJ
    Jan 5, 2022 at 10:40

1 Answer 1

0

Issue got solved by doing two things

  • Removed primary calling from
/**
 * Initialize the activation works of the plugin.
 */
function awraq_activate_plugin()
{
    //removed 
}

register_activation_hook(__FILE__, 'awraq_activate_plugin');

Added the call to "plugins_loaded"


/**
 * Initialize the plugin.
 */
function awraq_init_plugin()
{
    if (Init::activate() != TRUE) {
        deactivate_plugins(plugin_basename(__FILE__));
        //added below
        Notice::error('Plugin got Deactivated. Please check the dependencies.', true);
    }
}
add_action('plugins_loaded', 'awraq_init_plugin');
  • And on the class removed __CLASS__ with full namespace and class - private static $globalScopeName = 'Awraq\Base\Notice';, since it will get called from outside of this.
namespace Awraq\Base;

if (!defined('ABSPATH')) exit;

class Notice
{
    private static $globalScopeName = 'Awraq\Base\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(self::$globalScopeName, '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(self::$globalScopeName, '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(self::$globalScopeName, '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(self::$globalScopeName, '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));
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.