Changing the “Plugin Activated” Message Default

The question:

Whenever an administrator in WordPress activates a plugin, upon the reload of the plugin page, a notice will appear upon successful activation reporting “Plugin Activated”.

Screenshot of Plugin Activated message

Is there a way to change this text that appears inside the admin notice, or must I use my own custom message? Additionally, if I must use a custom message, will this suppress the default “Plugin Activated” message?

Related Questions:

Duplicate:

Thanks to Pieter for the find:

Additional Resources:

Note

Rememember that although ‘gettext’ filter is only applied during calls to the translate() function, translate() is used by virtually all other i18n functions in i18n.php. These include all of the functions listed here in this post on “Gettext Syntax“.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You can try this:

is_admin() && add_filter( 'gettext', 
    function( $translated_text, $untranslated_text, $domain )
    {
        $old = array(
            "Plugin <strong>activated</strong>.",
            "Selected plugins <strong>activated</strong>." 
        );

        $new = "Captain: The Core is stable and the Plugin is <strong>activated</strong> at full Warp speed";

        if ( in_array( $untranslated_text, $old, true ) )
            $translated_text = $new;

        return $translated_text;
     }
, 99, 3 );

to modify the message to your likings:

translated

We can refine it further:

If you only want to activate the filter on the /wp-admins/plugins.php page, you can use the following instead:

add_action( 'load-plugins.php',
    function(){
        add_filter( 'gettext', 'b2e_gettext', 99, 3 );
    }
);

with:

/**
 * Translate the "Plugin activated." string
 */
function b2e_gettext( $translated_text, $untranslated_text, $domain )
{
    $old = array(
        "Plugin <strong>activated</strong>.",
        "Selected plugins <strong>activated</strong>." 
    );

    $new = "Captain: The Core is stable and the Plugin is <strong>activated</strong> at full Warp speed";

    if ( in_array( $untranslated_text, $old, true ) )
        {
            $translated_text = $new;
            remove_filter( current_filter(), __FUNCTION__, 99 );
        }
        return $translated_text;
}

where we remove the gettext filter callback as soon we have a match.

If we want to check the number of gettext calls made, before we match the correct string, we can use this:

/**
 * Debug gettext filter callback with counter
 */
function b2e_gettext_debug( $translated_text, $untranslated_text, $domain )
{
        static $counter = 0;
        $counter++;

        $old = "Plugin <strong>activated</strong>.";
        $new = "Captain: The Core is stable and the Plugin is <strong>activated</strong> at full Warp speed";
        if ( $untranslated_text === $old )
        {
            $translated_text = $new;
            printf( 'counter: %d - ', $counter );
            remove_filter( current_filter(), __FUNCTION__ , 99 );
        }
        return $translated_text;
}

and I get 301 calls on my install:
301

I can reduce it to only 10 calls:

10

by adding the gettext filter within the in_admin_header hook, within the load-plugins.php hook:

add_action( 'load-plugins.php',
    function(){
        add_action( 'in_admin_header',
            function(){
                add_filter( 'gettext', 'b2e_gettext_debug', 99, 3 );
            }
        );
    }
);

Notice that this will not count the gettext calls before the internal redirect used when the plugins are activated.

To activate our filter after the internal redirect we can check the GET parameters used when plugins are activated:

/**
 * Check if the GET parameters "activate" and "activate-multi" are set
 */
function b2e_is_activated()
{
    $return         = FALSE;
    $activate       = filter_input( INPUT_GET, 'activate',       FILTER_SANITIZE_STRING );
    $activate_multi = filter_input( INPUT_GET, 'activate-multi', FILTER_SANITIZE_STRING );

    if( ! empty( $activate ) || ! empty( $activate_multi ) )
        $return = TRUE;

    return $return;
}

and use like this:

b2e_is_activated() && add_filter( 'gettext', 'b2e_gettext', 99, 3 );

in the previous code example.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment