• Resolved Denis ?oljom

    (@dingo_d)


    I am using plugin boilerplate to create a plugin and would like to add a message on plugin activation, instead of or under the ‘Plugin activated’ notice.

    I’ve tried every solution here, but nothing worked.

    I have

    
    register_activation_hook( __FILE__, 'activate_plugin' );
    
    function activate_plugin() {
      require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-activator.php';
      Plugin_Activator::activate();
    }

    I tried to implement this solution like this

    
    register_activation_hook( __FILE__, 'activate_plugin' );
    
    function activate_plugin() {
      require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-activator.php';
      Plugin_Activator::activate();
      add_action( 'load-plugins.php',
          function() {
              add_filter( 'gettext', Plugin_Activator::change_plugin_text(), 99, 3 );
          }
      );
    }
    

    In my class-plugin-activator.php file in Plugin_Activator class I have public static function activate() method that works (things get written in the db), and I created

    
    public static function change_plugin_text( $translated_text, $untranslated_text, $domain ) {
        $old = array(
            'Plugin <strong>activated</strong>.',
            'Selected plugins <strong>activated</strong>.',
        );
    
        $new = '<div class="notice notice-success is-dismissible"><p>' . __( 'This is my custom notice.' ) . '</p></div>';
    
        if ( in_array( $untranslated_text, $old, true ) ) {
          $translated_text = $new;
          remove_filter( current_filter(), __FUNCTION__, 99 );
        }
    
        return $translated_text;
      }
    

    But this is not working. So what am I doing wrong?

    How can I add admin notice in OOP way when activating a plugin?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here is a quick solution to get rid of default “Plugin activated” message and bring in your own message instead. In your theme’s functions.php file please add the following block of code.

    All we have done is modified the output of gettext hook.

    is_admin() && add_filter( 'gettext',
        function( $translated_text, $untranslated_text, $domain )
        {
            $old = array(
                "Plugin <strong>activated</strong>.",
                "Selected plugins <strong>activated</strong>."
            );
    
            $new = "Congratulations! Your plugin has been <strong>activated</strong> and ready for use!";
    
            if ( in_array( $untranslated_text, $old, true ) )
                $translated_text = $new;
    
            return $translated_text;
        }
    , 99, 3 );

    Let me know if this works for you.

    Thread Starter Denis ?oljom

    (@dingo_d)

    That’s basically what I did, but it’s not working. Plus I was hoping for a way to put this inside the Plugin_Activator class.

    I sorted this out be just adding admin_init hook in the root of my plugin that has admin_notices hook.

    The idea was to somehow wait or see if the activate() method will work before showing the notice.

    Dion

    (@diondesigns)

    Your code doesn’t execute the way you want because plugin activation occurs in the background, and WordPress does a redirect immediately after the activation code is run.

    If you want to change the activation message, you’ll need to set an option in your activation function, and then check for that option during plugin instantiation. If the option exists, it means the plugin was just activated, and you can then display your message. Make sure to delete the option after you act on it!

    Thread Starter Denis ?oljom

    (@dingo_d)

    Yeah, I added admin_init hook that hooks admin_notices hook that checks for an option if the plugin is activated and then shows the notice.

    I also added delete option in register_deactivation_hook.

    May not be ‘pretty’ but it works.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add additional admin notice on plugin activation in OOP’ is closed to new replies.