Fatal Error in Button Block with PHP 8+ – $b[‘width’] Access on String
-
Hi,
We’ve run into a fatal error with the Button Block in the latest version of Ultimate Blocks when running on PHP 8+.
Error message:
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in
/wp-content/plugins/ultimate-blocks/src/blocks/button/block.php:124Code in question (line 124 of block.php):
$width_styles[‘width’] = Ultimate_Blocks\includes\spacing_preset_css_var($b[‘width’][‘all’]);
$b['width']is sometimes stored as a string instead of an array (likely in older saved blocks or malformed block attributes).
PHP 8+ throws a fatal error when trying to access an array key (['all']) on a string.
This can happen with older posts/pages that contain Button Blocks created in earlier versions of Ultimate Blocks.Proposed fix:
A simple type check prevents the fatal error:if (
$buttonWidth === ‘fixed’ &&
!empty($b[‘width’]) &&
is_array($b[‘width’]) &&
isset($b[‘width’][‘all’])
) {
$width_styles[‘width’] = Ultimate_Blocks\includes\spacing_preset_css_var($b[‘width’][‘all’]);
}Steps to reproduce:
- Create a Button Block in an older version of Ultimate Blocks.
- Update to the latest plugin version and run on PHP 8.1 or newer.
- Load the page without re-saving the block in the editor.
- Fatal error occurs.
Suggested resolution:
- Add the
is_array()check before accessing$b['width']['all']inub_multi_buttons_parse()inblock.php. - Consider validating or normalizing
$b['width']when parsing saved block attributes.
This would make the block much more robust and backward-compatible with older saved content, preventing fatal errors in PHP 8+.
The topic ‘Fatal Error in Button Block with PHP 8+ – $b[‘width’] Access on String’ is closed to new replies.