DISALLOW_FILE_EDIT constant being ignored

This appeared to be the result of (what I assume) was the User Role Editor plugin being updated, and then directly affecting the wp_user_role field on the options table of the database, which somehow led to DISALLOW_FILE_EDIT being ignored.

I had to take several steps to fix this, as there were custom user roles I had created, as well as roles added by WooCommerce. The steps I took:

  • Copied the wp_user_roles field from the database, so that I had a serialised array of all the custom user roles to hand;
  • With the User Role Editor plugin active, went to Settings->User Role Editor, clicked on the Tools tab, then click the red Reset button (note the warnings about this resetting ALL user roles). After this was done, the plugin/theme file editor had disappeared – DISALLOW_FILE_EDIT was no longer being ignored.
  • Deactivated User Role Editor
  • Deactivated, then reactivated WooCommerce (to re-add the WooCommerce user roles like Customer & Shop Manager)
  • Wrote a custom plugin to add the roles that had previously been added by User Role Editor.

With regard to that last point: Because I’d saved a copy of the serialised array, I simply unserialised it and used the role information therein to create the custom roles I needed:

//  Activate the plugin

function myPlugin_install() {
    addCustomRoles();
}

register_activation_hook( __FILE__, 'myPlugin_install');

//  Add the custom roles

function addCustomRoles() {
    $userRolesArray = array();

    $userRolesArray['content_management'] = array(
        'name'  => 'Content Management',
        'capabilities'  => array(
            'level_0' => true,
            'level_1' => true,
            'level_2' => true,
            'level_3' => true,
            'edit_published_pages' => true,
            'edit_published_posts' => true,
            'edit_published_products' => true,
        )
    );

    $userRolesArray['content_consumer'] = array(
        'name'  => 'Content Consumer',
        'capabilities'  => array(
            //  etc.
        )
    );

    foreach($userRolesArray as $role=>$detailsArray) {

        $result = add_role(
            $role,
            $detailsArray['name'],
            $detailsArray['capabilities']
        );
    }
}

Then I deactivated & reactivated my custom plugin and boom, all of the user roles were back as before, and the file editor is disabled across all roles once more.