Home>Support>How to pass data in the_widget() ?

How to pass data in the_widget() ?

By aboutsam, 6 years ago. Last reply by Alex S, 6 years ago.
Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Hey guys, i created a plugin as you explained in your tutorial site. Everything work fine but i want to insert the widget also in my single-pages, which i only can add in the template files.

For my project, i need to update the single-event.php file and i tried to insert the plugin via the function the_widget().
<?php the_widget(‘Event_Widget’, $instance, $args); ?>
My $instance is an array and had all variables which i also match to the tpl via get_template_variables() function.

My array in the widget file:

return array(
‘headline’ => $instance[‘layout’][‘headline’],
‘headlineSize’ => $instance[‘layout’][‘headlineSize’],
‘body’ => $instance[‘layout’][‘body’],
‘txtPos’ => $instance[‘layout’][‘position’],
‘bgColor’ => $instance[‘layout’][‘bg_color’],
‘hl_color’ => $instance[‘layout’][‘hl_color’],
‘buttonTxt’ => $instance[‘button’][‘text’],
‘event’ => $instance[‘event’][‘category’],
‘layout’ => $instance[‘event’][‘layout’],
‘order’ => $instance[‘event’][‘order’],
);

So i create a $instance with …

$instance = array(
‘headline’ => ‘My Headline’,
‘headlineSize’ => ‘h2’,
‘body’ => ‘This is the Body’,
‘txtPos’ => ‘center’,
‘bgColor’ => ‘#FFF’,
‘hl_color’ => ‘#333’,
‘buttonTxt’ => ‘My Button’,
‘event’ => ‘festival’,
‘layout’ => 2,
‘order’ => ‘asc’,
);

At the end, i see the widget but nothing of my arguments was set to the widget. My widget come with the default settings.

How do i store the $var into my widget to get the output?

Thanks a lot for help!

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 6 years, 7 months ago Alex S
    Hi, I Work Here

    Hi Aboutsam,

    Data is passed to the widget in a similar manner as it’s structured in the widget itself. For example, if you want to pass the headline text, you’ll need to pass:

    'headline' => array(
        'text' => 'This is the headline text';
    ),
    

    You’ll need to pass more than that for the widget to display, but the above example PHP should give you a better idea of how to proceed.

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More