• Resolved Fixmysite

    (@newtlabs)


    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:124

    Code 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:

    1. Create a Button Block in an older version of Ultimate Blocks.
    2. Update to the latest plugin version and run on PHP 8.1 or newer.
    3. Load the page without re-saving the block in the editor.
    4. Fatal error occurs.

    Suggested resolution:

    • Add the is_array() check before accessing $b['width']['all'] in ub_multi_buttons_parse() in block.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+.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Farhan Shafi

    (@farhanshafi)

    Hi @newtlabs Thank you so much for bringing this to our attention and for providing a clear explanation along with a proposed fix, we really appreciate it!

    We’ve actually already addressed this issue, and the fix will be included in the next release.

    Thanks again for your helpful input.

    Best regards

    Plugin Contributor Farhan Shafi

    (@farhanshafi)

    @newtlabs Let me know, If you are still facing the error.

    Kind Regards
    Farhan

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Fatal Error in Button Block with PHP 8+ – $b[‘width’] Access on String’ is closed to new replies.