globals - 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 sideb

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="<?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.

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="<?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.

Share Improve this question edited Aug 25, 2019 at 20:44 Glorfindel 6093 gold badges10 silver badges18 bronze badges asked Aug 12, 2012 at 21:39 JasonJason 6062 gold badges11 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
      }

    }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216046a4617026.html

相关推荐

  • globals - 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 sideb

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信