1

I encounter a weird error while using the register_activation_hook and namespace.

The plugin generated 236 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.

I get this error even with this simple plugin:

<?php
/**
 * Plugin Name: Sample Plugin
 *
 */
namespace Sample;

register_activation_hook( __FILE__, 'sp_activate' );
function sp_activate() {

}

If I remove the namespace the error does not occur. Do I miss something? What is wrong here?

  • That's the entirety of the "plugin" that is causing the message you are seeing? – Will the Web Mechanic Jun 29 '14 at 3:49
  • I don't understand your question. Can you differently ask it? – obstschale Jun 29 '14 at 7:57
3

When we are outside the Sample namespace, we should call the function by \Sample\sp_activate(), so please try the following:

<?php
/**
 * Plugin Name: Sample Plugin
 * Version:     0.0.2
 */

namespace Sample;

\register_activation_hook( __FILE__, '\Sample\sp_activate' );

function sp_activate() {

}

If something is printed out during the plugin activation it will result in unexpected output message. If you use for example echo 'Hello'; in your activation function, it will generate such a message of 5 characters.

You should consider using WP_DEBUG_LOG and log the errors into a file, to see what kind of errors you're getting there.

I hope this helps.

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.