最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - Unable to show a message after plugin activation

programmeradmin1浏览0评论

Based on:

My actual code

class Activation {

  function __construct() {

    add_action( 
      'admin_notices', 
      array($this,"text_admin_notices")
    );

    Utils::update_option("is_activated", true);
  }

  function text_admin_notices () {
    ?>
        <div class="notice notice-success is-dismissible">
            <p> TEST MESSAGE</p>
        </div
    <?php
  }

}

I know that __construct() is being executed, because Utils::update_option (a simple wrapper) is working, it's creating the option in the option table.

So I expect that the call to add_action should show a message to the admin user that is activating my plugin.

Actually, the plugin is beign activated, but no message is shown.

I'm using Wordpress 4.7.1 (a fresh, clean, no-other-stuff, today's installation)

What's wrong in my code?

Based on: https://codex.wordpress/Plugin_API/Action_Reference/admin_notices

My actual code

class Activation {

  function __construct() {

    add_action( 
      'admin_notices', 
      array($this,"text_admin_notices")
    );

    Utils::update_option("is_activated", true);
  }

  function text_admin_notices () {
    ?>
        <div class="notice notice-success is-dismissible">
            <p> TEST MESSAGE</p>
        </div
    <?php
  }

}

I know that __construct() is being executed, because Utils::update_option (a simple wrapper) is working, it's creating the option in the option table.

So I expect that the call to add_action should show a message to the admin user that is activating my plugin.

Actually, the plugin is beign activated, but no message is shown.

I'm using Wordpress 4.7.1 (a fresh, clean, no-other-stuff, today's installation)

What's wrong in my code?

Share Improve this question asked Jan 12, 2017 at 11:05 realteborealtebo 2672 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
<?php
/*
  Plugin Name: Activation
  Description: This display notice message test plugin
  Author: Nanhe Kumar
  Version: 1.0
  Author URI: http://nanhe.in/
 */

class Activation {

    public static function init() {
        add_action('admin_notices', array(__CLASS__, 'text_admin_notice'));
    }

    public static function text_admin_notice() {
        ?>
        <div class="notice notice-success is-dismissible">
            <p> TEST MESSAGE</p>
        </div>
        <?php
    }

}
add_action('init', array('Activation', 'init'));

Your message not showing because your class constructor is not executing you can test through add die in your constructor then you can better understand what is happening.

发布评论

评论列表(0)

  1. 暂无评论