Capabilities don't add

时间:2014-07-01 作者:Ehsaan

我写了这段代码。

function wpo_install() {
    update_option( \'wpo_version\', WPO_VERSION );

    wpo_add_caps();
    wpo_add_roles();

    set_transient( \'_wpo_activation_redirect\', true );
}
register_activation_hook( WPO_ROOT, \'wpo_install\' );
在另一个文件中:

global $wpo_shared_caps;
$wpo_shared_caps = array(
    \'edit_wp_orders\',
    \'edit_wp_order\',
    \'edit_others_wp_orders\',
    \'read_wp_order\',
    \'read_private_wp_orders\',
    \'delete_wp_order\'
);

function wpo_add_caps() {
    global $wpo_shared_caps;

    $admin_caps = array(
        \'backup_settings\',
        \'view_full_reports\',
        \'get_report_output\'
    );
    $admin_caps = array_merge( $admin_caps, $wpo_shared_caps );
    $admin = get_role( \'administrator\' );

    foreach( $admin_caps as $admin_cap ) 
        $admin->add_cap( \'administrator\', $admin_cap );

}

function wpo_add_roles() {
    remove_role( \'accountant\' );
    remove_role( \'employer\' );

    global $wpo_shared_caps;

    $read_caps = array(
        \'read_wp_order\'              => true,
        \'read_private_wp_orders\'     => true
    );

    $acc_caps = array();
    foreach( $wpo_shared_caps as $cap )
        $acc_caps[ $cap ] = true;

    add_role( \'accountant\', __( \'Accountant\', \'wpo\' ), $acc_caps );
    add_role( \'employer\', __( \'Employer\', \'wpo\' ), $read_caps );
}
但这些都不起作用。请帮帮我。我弄糊涂了!

1 个回复
最合适的回答,由SO网友:John Blackbourn 整理而成

您的wpo_add_caps() 功能不正确:

$admin->add_cap( \'administrator\', $admin_cap );

其内容应为:

$admin->add_cap( $admin_cap );

资料来源:WP_Role::add_cap().

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?