PHP Fatal Error Fix – Array to String Conversion in cap-helper.php
-
Issue: The plugin crashes with “Array to string conversion” and “Illegal offset type” errors on lines 65-66 of cap-helper.php when other plugins (like Bricks Builder) incorrectly set post type capability values as arrays instead of strings.
Root Cause: Some third-party plugins don’t follow WordPress standards and set
$wp_post_types[type]->cap->capability_nameas arrays rather than strings, breaking the assumption in your force_distinct_post_caps() method.
Error MessageError Type:
- PHP Warning: Array to string conversion (line 65)
- PHP Fatal error: Uncaught TypeError: Illegal offset type in isset or empty (line 66)
Location:
- File: /wp-content/plugins/capabilities-pro/includes/cap-helper.php
- Method: CME_Cap_Helper->force_distinct_post_caps()
Call Stack:
CME_Cap_Helper->force_distinct_post_caps() (line 24)
CME_Cap_Helper->refresh() (line 20)
CME_Cap_Helper->__construct() (functions.php:59)
Root Cause: Third-party plugins setting post type capabilities as arrays instead of strings, breaking the assumption in array_unique((array) $type_obj->cap) and subsequent isset() operations.Recommended Fix: Add type validation before processing capabilities to filter out non-string values. The attached code change adds proper type checking to prevent fatal errors while maintaining functionality.
Impact: This affects any site using plugins that don’t properly handle WordPress capability structures, causing complete site crashes.
The fix I implemented resolves the immediate error while maintaining all the plugin’s functionality.
Anyone having this issue can replace the code in cap-helper.php line 63-73 with the code below.
// count the number of post types that use each capability
foreach( $wp_post_types as $post_type => $type_obj ) {
// Convert cap object to array and filter out non-string values to prevent array-to-string conversion errors
$caps_array = (array) $type_obj->cap;
$string_caps = array();
foreach( $caps_array as $cap_key => $cap_value ) {
// Only process string capability values - skip arrays or objects that some plugins incorrectly set
if ( is_string( $cap_value ) && !empty( $cap_value ) ) {
$string_caps[] = $cap_value;
}
}
foreach( array_unique( $string_caps ) as $cap_name ) {
if ( ! isset( $this->all_type_caps[$cap_name] ) ) {
$this->all_type_caps[$cap_name] = 1;
} else {
$this->all_type_caps[$cap_name] = (int) $this->all_type_caps[$cap_name];
$this->all_type_caps[$cap_name]++;
}
}
}Please note the fix was AI-assisted, but I hope it makes sense and helps pinpoint the issue for the next update.
The topic ‘PHP Fatal Error Fix – Array to String Conversion in cap-helper.php’ is closed to new replies.