Pass variables from one widget to another widget

I wrote a widget but I need to use the variables from one widget in another…both widgets are in the same dynamic sidebar.

This is what I am trying to do: I wrote a widget that adds a footer to the bottom of an image slider widget. When I add the footer widget under the slider widget…the footer shows up.

The problem is that I need the footer widget to appear within the slider < div > set, not under it. For example:

<div id="this-is-the-slider-widget">
    <div>do slider stuff here</div>
    <div id="I-want-the-footer-widget-to-start-here">
        <div>display footer widget here</div>
    </div><!-- close footer widget-->
</div><!-- close slider widget -->

However; the footer widget displays right after the slider widget is done.

<div id="this-is-the-slider-widget">
    <div>do slider image show</div>
</div><!-- cose slider widget -->
<div id="footer-widget">
    <div>display footer here</div>
</div><!-- close footer widget -->

Otherwise; the bottom css of the slider pushes the footer down and messes things up. If I adjust the css so the footer and slider have a zero bottom margin and padding then the content below the banner has no spacing when the footer widget is not present.

This is how I want it to look, flush:

…but widgets are executed in order so this is what happens:

I could solve the problem by using two css classes and check if the widget exists and run .yes-widget {} or .no-widget{} but I think it’s is a lot cleaner to just be able to pass the variables from one widget to another.

How I got the first image to work (but none of the variables form the footer widget are passed)…I included the footer html within the slider’s widget like this:

<div id="this-is-the-slider-widget">
    <div>do slider stuff here</div>

    <div id="display-footer-here>
        <?php global $ct_on, $child_dir;
            if($ct_on && file_exists($child_dir.'/library/includes/yif-top-banner-section-footer.php')) {
                include_once ($child_dir.'/library/includes/yif-top-banner-section-footer.php');
         } ?>
    </div><!-- end footer widget -->
</div><!-- end slider widget -->

This is the footer html that is included:

<div class="top-banner-section-footer">
    <div class="top-banner-section-footer-wrapper">
        <a href="https://wordpress.stackexchange.com/questions/61625/<?php echo $fltitle;?>" class="footerbox left-box no-underline">
            <h3><?php echo $fltitle;?></h3>
            <p><?php echo $fltext;?></p>
        </a>

        <a href="<?php echo $fmlink;?>" class="footerbox middle-box no-underline">
            <h3><?php echo $fmtitle;?></h3>
            <p><?php echo $fmtext;?></p>
        </a>

        <a href="<?php echo $frlink;?>" class="footerbox right-box no-underline">
            <h3><?php echo $frtitle;?></h3>
            <p><?php echo $frtext;?></p>
        </a>
    </div><!-- /.top-banner-section-footer-wrapper -->
</div><!-- /.top-banner-section-footer -->

Unless you have a better way then included the footer html right where I want the footer to execute…how do I get the variables form the footer widget to work in in the included footer html file?

For example, the footer widget saves this variable: $frlink. How do I pass $frlink to the included file?

I have tried GLOBAL but I am not sure which function of the slider widget to add it.
For example, the function that has the widget Constructor, where it prints the widget, saves the widget, the widget form in the backend , etc.

1 Answer
1

You don’t need to make it global.

If your include a file from within a functions scope, any variables that you defined in that function (before that point) will be exposed in the included file as well.

But because the variable you are talking about represents the state of the footer file inclusion (if I understood correctly), you can make it a static variable:

class You_Widget{

    protected static
      $footerIncluded = false;

    public function widget(){

      // this will only run once
      if(!self::$footerIncluded){        
        include __DIR__ . '/footer.php';
        self::$footerIncluded = true;
      }

    }

}

Leave a Comment