dongyu9850 2018-10-02 03:06
浏览 62
已采纳

该插件在激活期间生成了2651个意外输出字符,为什么? [重复]

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>
  • 写回答

1条回答 默认 最新

  • dongtangu8403 2018-10-02 03:36
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值