0
votes

I was try many answred question here... Like remove space before <?php and after ?>

not working at all Iam still stucked..

My code below..

add_action( 'admin_notices', 'fii' );
function fii(){
    // parent plugin
    if ( ! is_plugin_active( 'xxx/aaa.php' ) and current_user_can( 'activate_plugins' ) ) {
        echo '<div class="notice notice-warning"><p>You need install xxxx plugin</p></div>';
    }
}
register_activation_hook( __FILE__, 'fii' );

The problem is when active my plugin, wordpress give me error info like this..

"The plugin generated 281 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin."

But when i close my echo inside fii function, its fine"

   add_action( 'admin_notices', 'fii' );
    function fii(){
        // parent plugin
        if ( ! is_plugin_active( 'xxx/aaa.php' ) and current_user_can( 'activate_plugins' ) ) {
            //echo '<div class="notice notice-warning"><p>You need install xxxx plugin</p></div>';
        }
    }
    register_activation_hook( __FILE__, 'fii');

WHat wrong with my echo ??

2
Your problem must lie elsewhere, the code is valid, and your echo is not 281 characters.WheatBeak
why are you assigning fii it to admin_notices as well as the activation hook?inarilo

2 Answers

0
votes

My guess is that since you are calling this plugin both on the activation hook and as admin notices, that as soon as the plugin is activated the admin_notices hook is called (well in the same cycle). As a result your plugin is throwing out the echo at a point during the activation process. As such this is unexpected behaviour since an activation isnt expecting to echo out anything.

I also don't see any need for this function to run on activation. Only call it on admin_notices and if necessary adapt your logic in the function so that this notice only shows once the plugin has been activated.

Additionally, at present your code isn't taking account of whether or not the 3rd party plugin is active or even installed. So, users would get that message even if they had this other plugin installed and activated.

There is already a great library for adding required and recommended plugins to WordPress installation called TGM Plugin Activation. I'd strongly recommend that you use that instead of creating your own logic.

0
votes

The most common causes are:

  1. A white space before or after the PHP opening or closing tags
  2. A file encoded in UTF-8
  3. Another issue when something is called at the wrong time, or a call that cannot be resolved without intervention
  4. Using the WordPress add_option function. Switching to update_option instead can resolve the problem.

I believe it might be the UTF-8 encoding try changing it to ANSI.