This question already has an answer here:
I am trying to develop a plugin. When I activate the plugin it's showing me following error message:
The plugin generated 2651 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 can not understand why it's showing this error. I can see there is no white space to show this error!
Here is my code:
<?php
/*
Plugin Name: Forum Roles
Plugin URI: http://www.creativeartbd.com
Description: Generate a google map
Version: 1.0
Author: Shibbir
Author URI: http://creativeartbd.com/bio/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: gmape
Domain Path: /languages/
*/
// custom forum roles and capabilites class
class BOJ_Forum_Roles {
function __construct() {
// register plugin activation hook
register_activation_hook( __FILE__, 'activation' );
// register plgin deactivation hook
register_deactivation_hook( __FILE__, 'deactivation' );
}
// plugin activation method
function activation () {
// get the default administrator tole
$role =& get_role('administrator');
// add forum capabilities to the administrator role
if( !empty($role) ) {
$role->add_cap('publish_forum_topics');
$role->add_cap('edit_others_forum_topics');
$role->add_cap('delete_forum_topics');
$role->add_cap('read_forum_topics');
}
// create the forum administator tole
add_role( 'forum_administrator', 'Forum Administrator', array(
'publish_forum_topics', 'edit_others_forum_topics', 'delete_forum_topics', 'read_forum_topics'
) );
// create the moderator role
add_role('forum_moderator', 'Forum Moderator', array(
'publish_forum_topics', 'edit_others_forum_topics','read_forum_topics'
));
// create the forum member role
add_role('forum_member', 'Forum Member', array(
'publish_forum_topics', 'read_forum_topics'
));
// create the forum suspended role
add_role( 'forum_suspended', 'Forum Suspeded', array(
'read_forum_topics'
) );
}
// plugin deactivation method
function deactivation () {
// get the default administrator role
$role =& get_role('administrator');
//remove forum capabilites to the administrator role
if(!empty($role)) {
$role->remove_cap('publish_forum_topics');
$role->remove_cap('edit_others_forum_topics');
$role->remove_cap('delete_forum_topics');
$role->remove_cap('read_forum_topics');
}
// set up an array of roles to delete
$roles_to_delete = array(
'forum_administrator',
'forum_moderator',
'forum_member',
'forum_suspended'
);
// loop through each role, deleting the role if necessary
foreach ($roles_to_delete as $role) {
// get the users of the role
$users = get_users(array(
'role' => $role
));
// check if there are no users for the role
if( count($users) <= 0 ) {
//remove the role from the site
remove_role( $role );
}
}
}
}
$forum_roles = new BOJ_Forum_Roles();
Can you tell me how can I solve it?
</div>
For this error
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'activation' not found or invalid function name ..
You'll wan't to call the method of your object (class). To do that you can pass an array to the callback, the first element is the class name (in static calls) or an instance of the object, the second element is the method name such as:
class BOJ_Forum_Roles {
public function __construct() {
register_activation_hook( __FILE__, [$this,'activation'] );
register_deactivation_hook( __FILE__, [$this,'deactivation'] );
}
public function activation(){...}
public function deactivation(){...}
}
In core PHP call_user_func_array()
is called like this
call_user_func_array([$this,'activation'], $args);
http://php.net/manual/en/function.call-user-func-array.php
It's possible output from this error is giving you the original error you were seeing.
Static Calls
Just for completeness if you had
public static function activation(){...}
You would call it this way (static)
class BOJ_Forum_Roles {
public function __construct() {
register_activation_hook( __FILE__, [__CLASS__,'activation'] );
register_deactivation_hook( __FILE__, [__CLASS__,'deactivation'] );
}
public static function activation(){...}
public static function deactivation(){...}
}
Using the __CLASS__
constant is preferable to putting in the class name, in PHP5.5+ you can also do it this way self::class
, although I believe the constant is a bit faster. I think the same holds true for static::class
which would be late static binding. And of course you can always do this (in 5.5+) BOJ_Forum_Roles::class
which in this example makes little sense, but it can be useful with namespaces.
UPDATE
showing...Notice: Only variables should be assigned by reference in
$role =& get_role('administrator');
Remove the Amp, as you are assigning the results of a function by reference. I am not certain, but if it's an object it's passed by reference anyway. It dosn't matter because you probably shouldn't be modifying it in your function and in fact this just caught my attention (in deactivation):
$role =& get_role('administrator');
...
foreach ($roles_to_delete as $role) {
...
}
You are re-assigning the $role
variable in the loop. This may or may not cause issues (I can't tell for sure without testing it), but overwriting it is probably a simple over site on your part. It's generally bad practice to overwrite a variable this way (as the assignment of a loop) if it's been previously declared as often this would not be an intentional thing.
Cheers!