How to troubleshoot WordPress plugin activation errors

Solution 1:

My guess is that because you’re calling this function both on the init hook and on admin views, the admin_notices hook gets triggered during plugin activation. This causes your plugin to output a notice while it’s being activated, which is unexpected—init isn’t supposed to echo anything.

I also don’t see a need for this function to run on init. Instead, you could call it on admin_notices and adjust the logic so that the notice only displays once the plugin has been activated.

Additionally, your current code doesn’t check whether the other plugin is active or even installed, so users would see the message regardless of whether that plugin is present.

There’s already a well-established library for requiring and recommending plugins in WordPress called TGM Plugin Activation. I strongly suggest using that instead of writing your own custom logic.

Solution 2:

The most common causes of this issue are:

  1. Extra whitespace before in your PHP files.
  2. File encoded in UTF-8 with BOM instead of plain UTF-8 or ANSI.
  3. Code running at the wrong time, or calling a function that cannot be resolved without intervention.
  4. Using the WordPress add_option() function incorrectly—switching to update_option() may resolve the problem.

In your case, it’s likely due to UTF-8 encoding. Try converting the file to ANSI and see if that fixes the issue.