Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The class "My_Plugin" initializes my plugin and the class "My_Plugin_Notices" is supposed to output various admin notices with its methods.

I set the property "admin_notices" in "My_Plugin" as an instance of "My_Plugin_Notices." I then use the "activation_notice" and "deactivation_notice" in the appropriate "admin_notices" hooks. However... this doesn't work. Why? I have tried print, echo, and return statments in the notice methods.

Thoughts and/or suggestions?

class My_Plugin_Notices {
    public function activation_notice() {
        print '<div class="updated fade"><p>My_Plugin Activated!</p></div>';
    }

    public function deactivation_notice() {
        print '<div class="updated fade"><p>My_Plugin Deactivated!</p></div>';
    }
}

class My_Plugin {
    __construct() {

        $this->admin_notices = new My_Plugin_Notices;

        // Activation
        register_activation_hook( __FILE__, array( $this, 'activate' ) );

        // Deactivation
        register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
    }

    public function activate() {
        // Show activation confirmation message
        add_action( 'admin_notices', array( $this->admin_notices, 'activation_notice' ) );
    }

    public function deactivate() {
        // Show deactivation confirmation message
        add_action( 'admin_notices', array( $this->admin_notices, 'deactivation_notice' ) );
    }
}

$GLOBALS['my_plugin'] = new My_Plugin();
share|improve this question
    
admin_notices doesn't work because a plugin activation results in a redirect. This means that you'll have hooked your notices up, but then the page is reloaded and WordPress forgets all about it. WordPress deals with this by adding a query arg to the URL in the admin. I've used this technique to output messages on top of the Post Edit Screen. Not sure if this works on the Plugin Page too. Maybe it's simpler to use Feature Pointers instead? –  Fränk Apr 30 '13 at 9:47
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.