0

I have a class within a plugin which is loaded via the below code:

<?php

add_action(
    'plugins_loaded',
    array ( 'Company_Store_Locator', 'init' )
);

class Company_Store_Locator{

public static function init() {
    $class = __CLASS__;
    new $class;
}

And I have a constructor with a number of add_actions which are within my construct - these all work fine, however when I try and do_action for one of these nothing appears to happen. I am trying to return an admin notice only if certain criteria within a function occurs. The function is running as expected but nothing happens for do_action as I need to pass a variable to the admin notice function I cannot use add action here.

public function __construct() {

    add_action( 'admin_notices', array($this,'sample_admin_notice__success'));

Inside my public function create_tables() function:

$message = 'Tables created successfully';

do_action( 'sample_admin_notice__success', $message);

How would I get the do action to run in this scenario?

  • Have you tried if other actions work? Few days ago I've seen some question here and OP had some problems with admin notices as well, so maybe the problems lies in notices and not in actions? – Krzysiek Dróżdż Feb 6 '19 at 16:26
  • @KrzysiekDróżdż I can make an add_action work for notices, so if I want to just do one on plugin init, but not the do_action where I run something specific and need to pass the variable. – Aravona Feb 6 '19 at 16:35
  • If not done already, setting debug on might give you more clues. Add this to your wp-config.php file define( 'WP_DEBUG', true ); Make sure your sample_admin_notice__success is publicly accessible and the php file with the class is included. – Gert Mar 3 '19 at 22:21
  • @Gert it is accessible, if I switch to a add_action it works fine but fires the notice at the wrong point (init) and I cannot pass the message variable. – Aravona Mar 4 '19 at 9:16

Your Answer

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

Browse other questions tagged or ask your own question.