11

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".

14

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.

  • 1
    You nailed it, the gettex function. Excellent move – Pieter Goosen Mar 29 '14 at 11:39
  • Why did you chain together is_admin() and add_filter() with a logical && operator? Is there some sort of short-circuit evaluation going on where if the user is not an administrator, then add_filter won't run? – gate_engineer Mar 29 '14 at 17:48
  • 1
    Excellent answer! +1 and I'd +10 it for keeping an eye on performance with the get_text filter. – kaiser Apr 1 '14 at 19:51
  • 1
    yes that should work in a similar way @blackhawk – birgire Oct 15 '16 at 11:30
  • 1
    The problem is the redirect. Check 'plugin' GET parameter? If not available maybe hook into check_admin_referer and target 'activate-plugin_' . $plugin to write an option that says it's been activated and then delete it again on plugins.php page load. Maybe the recently_activated option can help? Might be better served as a new question @Omer – birgire Aug 3 '17 at 17:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.