Jordi
Jordi

Reputation: 53

Must be of type array, string given

i'm trying to create a custom elementor widget; however the add_link_attributes do not seem to support a string for a link. As the following error popped up:

Must be of the type array, string given

How can i turn this string, saved in $course_link turn into 'the type array'?

protected function render() {
        $settings = $this->get_settings_for_display();
        $current_user = wp_get_current_user();
        $course_link = get_post_permalink( $settings['training']); 

        $this->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );

        //Als gebruiker géén toegang heeft tot de training plaats saleslink
        if (get_user_meta( $current_user->ID, 'course_' . $settings['training'] . '_access_from' , true ) || current_user_can('administrator')) {
            $this->add_link_attributes( 'button', $course_link );
            $this->add_render_attribute( 'button', 'class', 'elementor-button-link' );          
        }
        elseif ( ! empty( $settings['sales-link']['url'] ) && !get_user_meta( $current_user->ID, 'course_' . $settings['training'] . '_access_from' , true )) { 
            $this->add_link_attributes( 'button', $settings['sales-link'] );
            $this->add_render_attribute( 'button', 'class', 'elementor-button-link' );
        }

        if ( ! empty( $settings['size'] ) ) {
            $this->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
        }

        $this->add_render_attribute( 'button', 'class', 'elementor-button' );
        $this->add_render_attribute( 'button', 'role', 'button' );

        if ( $settings['hover_animation'] ) {
            $this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
        }

        ?>
        <div <?php echo $this->get_render_attribute_string( 'wrapper' ); ?>>
            <a <?php echo $this->get_render_attribute_string( 'button' ); ?>>
                <?php $this->render_text(); ?>
            </a>
        </div>
        <?php 
    }

Upvotes: 3

Views: 2002

Answers (1)

Robin Gillitzer
Robin Gillitzer

Reputation: 1602

You can typecast a string (or whatever) to an array like this:

$course_link = (array) $course_link;

So in your function you could edit the fourth line to:

$course_link = (array) get_post_permalink( $settings['training'] );

Or the line eight to this:

$this->add_link_attributes( 'button', (array) $course_link );

Here you can read more

Upvotes: 1

Related Questions