SOLVED: Fatal Error - operator not supported for strings

Bricks Version: 1.5.3 (also happens on 1.5.2)
Browser: Chrome 100
OS: Linux.
URL: (a link to a page that illustrates the issue would be really helpful)
I was unable to open the page and it was throwing errors:

operator not supported for strings in /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/base.php:1718\nStack trace:\n#0 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/button.php(150): Bricks\\Element->set_attribute('_root', 'class', 'bricks-button')\n#1 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/base.php(2016): Bricks\\Element_Button->render()\n#2 /nas/content/live/cityverse/wp-content/themes/bricks/includes/frontend.php(390): Bricks\\Element->init()\n#3 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/container.php(558): Bricks\\Frontend::render_element(Array)\n#4 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/base.php(2016): Bricks\\Element_Container->render()\n#5 /nas/content/live/cityverse/wp-content/themes/bricks/includes/frontend.php(390): Bricks\\Element->init()\n#6 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/container.php(558): Bricks\\Frontend::render_element(Array)\n#7 /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/base.php(2016): Bricks\\Element_Container->render()\n#8 /nas/content/live/cityverse/wp-content/themes/bricks/includes/frontend.php(390): Bricks\\Element->init()\n#9 /nas/content/live/cityverse/wp-content/themes/bricks/includes/frontend.php(464): Bricks\\Frontend::render_element(Array)\n#10 /nas/content/live/cityverse/wp-content/themes/bricks/includes/frontend.php(718): Bricks\\Frontend::render_data(Array)\n#11 /nas/content/live/cityverse/wp-content/themes/bricks/page.php(7): Bricks\\Frontend::render_content(Array)\n#12 /nas/content/live/cityverse/wp-includes/template-loader.php(106): include('/nas/content/li...')\n#13 /nas/content/live/cityverse/wp-blog-header.php(19): require_once('/nas/content/li...')\n#14 /nas/content/live/cityverse/index.php(17): require('/nas/content/li...')\n#15 {main}\n thrown in /nas/content/live/cityverse/wp-content/themes/bricks/includes/elements/base.php on line 1718

I fixed the bug temporarily by adding the following code:

Before:

	/**
	 * Set HTML element attribute + value(s)
	 *
	 * @param string       $key         Element identifier.
	 * @param string       $attribute   Attribute to set value(s) for.
	 * @param string|array $value       Set single value (string) or values (array).
	 *
	 * @since 1.0
	 */
	public function set_attribute( $key, $attribute, $value = null ) {
		if ( is_array( $value ) ) {
			foreach ( $value as $val ) {
				$this->attributes[ $key ][ $attribute ][] = $val;
			}
		} else {
			if ( empty( $value ) ) {
				$this->attributes[ $key ][ $attribute ] = '';
			} else {
				$this->attributes[ $key ][ $attribute ][] = $value;
			}
		}
	}

After

	/**
	 * Set HTML element attribute + value(s)
	 *
	 * @param string       $key         Element identifier.
	 * @param string       $attribute   Attribute to set value(s) for.
	 * @param string|array $value       Set single value (string) or values (array).
	 *
	 * @since 1.0
	 */
	public function set_attribute( $key, $attribute, $value = null ) {
		
		if(!is_array($this->attributes[ $key ][ $attribute ])) {
			$this->attributes[ $key ][ $attribute ] = [];
		}

		if ( is_array( $value ) ) {
			foreach ( $value as $val ) {
				$this->attributes[ $key ][ $attribute ][] = $val;
			}
		} else {
			if ( empty( $value ) ) {
				$this->attributes[ $key ][ $attribute ] = '';
			} else {
				$this->attributes[ $key ][ $attribute ][] = $value;
			}
		}
	}

[Please describe in as much detail as possible how we can replicate this bug]

1 Like

Hi Darko,
Thanks so much for your report!

What do I have to do to reproduce the error?

Best regards,
timmse

Hi @timmse,

This error is still there but i am not sure what is causing it. Do you want me to send anything? I can probably sent export of the page or what else is needed?

The above answer of mine fixes the issue.

It’s always good practice to check if the variable is array, you can’t just push elements into it if it’s not array or some empty value.

So far i have to modify this code on each release.

Best Regards,
Darko

Hi Darko,
Thanks for the solution suggestion… but I would still like to know what I have to do for the error to occur :slight_smile:

In other words: what did you do where before the error was issued?

Hey @timmse ,

Here is short loom on the issue:

The export will help you reproduce the issue:

Hopefully this will help to fix the issue.

Best Regards,
Darko

This is probably the best fix, it does not affect the string section ($this->attributes[ $key ][ $attribute ] = '';), we only do the safety check before Array push.

	/**
	 * Set HTML element attribute + value(s)
	 *
	 * @param string       $key         Element identifier.
	 * @param string       $attribute   Attribute to set value(s) for.
	 * @param string|array $value       Set single value (string) or values (array).
	 *
	 * @since 1.0
	 */
	public function set_attribute( $key, $attribute, $value = null ) {

		if ( is_array( $value ) ) {
			if ( ! is_array( $this->attributes[ $key ][ $attribute ] ) ) {
				$this->attributes[ $key ][ $attribute ] = [];
			}
			foreach ( $value as $val ) {
				$this->attributes[ $key ][ $attribute ][] = $val;
			}
		} else {
			if ( empty( $value ) && ! is_numeric( $value ) ) {
				$this->attributes[ $key ][ $attribute ] = '';
			} else {
				if ( ! is_array( $this->attributes[ $key ][ $attribute ] ) ) {
					$this->attributes[ $key ][ $attribute ] = [];
				}
				$this->attributes[ $key ][ $attribute ][] = $value;
			}
		}
	}

Hi @gdarko ,

Thank you very much for the help.

We will review if the code helps.

Back to your question, you encounter the issue because you used the Attribute to add CSS class on the “Email our media team” button

image

Instead, you should use CSS classes field.

image

Of course, we will add this to the bug tracker as well.

Regards,
Jenn

Hi Darko,
We’ve improved the attributes in Bricks 1.7.1, now available as a one-click update in your WordPress Dashboard.

Please let us know if you are still experiencing issues.

Best regards,
timmse