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.

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:

share|improve this question
    
Go and have a look at this question stackoverflow.com/q/14095642/1908141 –  Pieter Goosen Mar 29 at 5:44

1 Answer 1

up vote 7 down vote accepted

You can try this:

is_admin() && add_filter( 'gettext', 
    function( $translated_text, $untranslated_text, $domain )
    {
        $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;

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

share|improve this answer
    
You nailed it, the gettex function. Excellent move –  Pieter Goosen Mar 29 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 at 17:48
    
I only wanted to target the backend and not the frontend. I took the is_admin() backend check outside the filter so it wouldn't have to run multiple times. I updated the answer to target only the /wp-admins/plugins.php page. –  birgire Mar 30 at 1:40
    
Excellent answer! +1 and I'd +10 it for keeping an eye on performance with the get_text filter. –  kaiser Apr 1 at 19:51

Your Answer

 
discard

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

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