wordpress custom user capabilities and roles

admin

Administrator
Staff member
I am fairly new to wordpress and I am attempting to create custom capabilities and assign them to custom roles, however, I am stuck.

This is what I have but on calling the function within a plugin I get "Fatal error: Call to undefined function add_cap()":

Code:
function setupCapabilities() {
    //create custom capabilities
    add_cap('view_all_holds');
    add_cap('view_advertiser_lockouts');
    add_cap('view_overages');
    add_cap('view_all_booked');
    add_cap('create_makegood_order');
    add_cap('edit_reports');

    //array for custom admin capabilities
    $adminCaps = array(
        'view_all_holds' => true, 
        'view_advertiser_lockouts' => true, 
        'view_overages' => true, 
        'view_all_booked' => true, 
        'create_makegood_order' => true, 
        'edit_reports' => true
    );

    //create new role with custom capabilities
    add_role('bam_admin', 'BAM Administrator', $adminCaps);

    //get user id
    $userID = get_current_user_id();

    //if the user id is me then add new role
    if ($userID == 46) { //me
        $user = new WP_User($userID);
        $user->add_role('bam_admin');
    }

    //check if the role was successfully applied
    if (in_array('bam_admin', (array) $userID->roles)) {
        echo 'You have succesfully created and checked a role.';
    } else {
        echo 'Something messed up';
    }


}