Nico Weerheim
Nico Weerheim

Reputation: 33

Use PHP shortcode value as CSS value

I am facing the following problem. On my website I am trying to use a WordPress shortcode as input for a background color gradient.

The PHP is as follows:

function vip_relative_time() { 
    $post_date = get_the_time('U');
    $delta = time() - $post_date;
    $delta2 = intval(($delta/86400));
    return $delta2;
}
add_shortcode('relativetime', 'vip_relative_time'); 

This code returns the amount of days that is between today and the post date. The goal is that the section on my website has a color gradient based on the above calculated days.

enter image description here

So when the calculating returns 50, 50% of the section will be filled with the selected color. This seems to work when I am editing the page:

enter image description here

But when I publish the page the the gradient is gone an I get the following error:

Warning : Illegal string offset 'size' in /home/customer/www/nicow23.sg-host.com/public_html/wp-content/plugins/elementor/includes/base/controls-stack.php on line 1251

enter image description here

Anyone who can help me out to fix this problem? Do I maybe need to change the datatype of my shortcode?

Thanks in advance!

The code in controls-stack.php

    /**
 * Parse dynamic settings.
 *
 * Retrieve the settings with rendered dynamic tags.
 *
 * @since 2.0.0
 * @access public
 *
 * @param array $settings     Optional. The requested setting. Default is null.
 * @param array $controls     Optional. The controls array. Default is null.
 * @param array $all_settings Optional. All the settings. Default is null.
 *
 * @return array The settings with rendered dynamic tags.
 */
public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
    if ( null === $all_settings ) {
        $all_settings = $this->get_settings();
    }

    if ( null === $controls ) {
        $controls = $this->get_controls();
    }

    foreach ( $controls as $control ) {
        $control_name = $control['name'];
        $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );

        if ( ! $control_obj instanceof Base_Data_Control ) {
            continue;
        }

        if ( $control_obj instanceof Control_Repeater ) {
            if ( ! isset( $settings[ $control_name ] ) ) {
                continue;
            }

            foreach ( $settings[ $control_name ] as & $field ) {
                $field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
            }

            continue;
        }

        $dynamic_settings = $control_obj->get_settings( 'dynamic' );

        if ( ! $dynamic_settings ) {
            $dynamic_settings = [];
        }

        if ( ! empty( $control['dynamic'] ) ) {
            $dynamic_settings = array_merge( $dynamic_settings, $control['dynamic'] );
        }

        if ( empty( $dynamic_settings ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
            continue;
        }

        if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
            $parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );

            $dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
            
            
        if( is_array( $settings ) )
        {
            if ( $dynamic_property ) {
                $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
            } else {
                $settings[ $control_name ] = $parsed_value;
            }
        } else {
                $settings[ $control_name ] = $parsed_value;
        }
            
            
        }
    }

    return $settings;
}

Upvotes: 1

Views: 95

Answers (1)

joeb
joeb

Reputation: 877

On or before line 1251, you should be checking if $settings is an array before trying to set its keys. If you do a var_dump($settings) prior to line 1251. It will probably tell you that it is not an array, and instead a string.

should be like this:

if( is_array( $settings ) )
{
    //make sure we have an array first
    if ( $dynamic_property ) {
        $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
    } else {
        $settings[ $control_name ] = $parsed_value;
    }
} else {
    //do something else
}

Upvotes: 0

Related Questions