Viewing 2 reply threads
  • Author
    Posts
    • #1305
      Marcus
      Member

      Already you have made widgets amazing and so easy to create and use.

      But I did have a problem.

      I need to know the sidebar ID in my widget form, so that I can use it to retrieve some meta data using that sidebar id/title.

      Currently the form is created and then the drop down (widget-select.php) that lets you pick a widget. The widgets are then loaded via piklist(pathtowidget.php);
      This process is not passed the $this->id variable, which is available when the form is being created, but not when rendering the widget forms.

      So what I’ve done is change these lines: (in includes/class-piklist-universal-widget.php, in the function form)

      
      piklist::render('shared/widget-select', array(
        'instance' => $instance
        ,'widgets' => $this->widgets
        ,'name' => $this->widget_core_name
        ,'widget_name' => $this->widget_name
        ,'class_name' => piklist::dashes($this->widget_core_name)
      ));
      

      to these lines:

      
      piklist::render('shared/widget-select', array(
        'instance' => $instance
        ,'sidebarid'=> $this->id
        ,'widgets' => $this->widgets
        ,'name' => $this->widget_core_name
        ,'widget_name' => $this->widget_name
        ,'class_name' => piklist::dashes($this->widget_core_name)
      ));
      

      Basically passing on the new argument, sidebarid, so that widget-select.php will pass it on to the forms that it loads.

      Now I just need to add:

      global $sidebarid;
      

      to any of my widget forms and I have access to the parent sidebar.

      Hope this helps someone else, and when Kevin comes up for air <grin> one day could he please add this to the functionality. (AS I HATE CHANGING CORE CODE) Or maybe even add an action or filter.

      Thanks guys, hope it helps someone else.

      Marcus

    • #1306
      Marcus
      Member

      Ok, left out a couple of things and made a couple of mistakes, so I thought I would fix it, in case anyone needs to use this in the future.

      First in the parts/shared/widget-select.php file, you need to change the foreach loop from this:

      <?php foreach ($widgets as $w): 
      ?>
      
        <div class="hide-all <?php echo !empty($w['form_data']['width']) ? 'piklist-widget-width-' . $w['form_data']['width'] : ''; ?> <?php echo !empty($w['form_data']['height']) ? 'piklist-widget-height-' . $w['form_data']['height'] : ''; ?> <?php echo $class_name; ?>-form <?php echo $class_name; ?>-form-<?php echo $w['add_on'] . '--' . $w['name']; ?> <?php echo current(maybe_unserialize($instance[$widget_name])) == $widget_name . '--' . $w['add_on'] . '--' . $w['name'] ? $class_name . '-form-selected' : ''; ?> ">
          
          <?php do_action('admin_notices'); ?>
          
          <?php if (!empty($w['data']['description'])): ?>
        
            <p>
              <em><?php echo $w['data']['description']; ?></em>
            </p>
        
          <?php endif; ?>
      
          <?php if ($w['form']) piklist($w['form']; ?>
      
        </div>
      
      <?php endforeach; ?>
      

      to this:

      <?php foreach ($widgets as $w): 
      ?>
      
        <div class="hide-all <?php echo !empty($w['form_data']['width']) ? 'piklist-widget-width-' . $w['form_data']['width'] : ''; ?> <?php echo !empty($w['form_data']['height']) ? 'piklist-widget-height-' . $w['form_data']['height'] : ''; ?> <?php echo $class_name; ?>-form <?php echo $class_name; ?>-form-<?php echo $w['add_on'] . '--' . $w['name']; ?> <?php echo current(maybe_unserialize($instance[$widget_name])) == $widget_name . '--' . $w['add_on'] . '--' . $w['name'] ? $class_name . '-form-selected' : ''; ?> ">
          
          <?php do_action('admin_notices'); ?>
          
          <?php if (!empty($w['data']['description'])): ?>
        
            <p>
              <em><?php echo $w['data']['description']; ?></em>
            </p>
        
          <?php endif; ?>
      
          <?php if ($w['form']) piklist($w['form'],array('sidebarid'=>$sidebarid)); ?>
      
        </div>
      
      <?php endforeach; ?>
      

      All I changed was added an argument onto this piklist call:

          <?php if ($w['form']) piklist($w['form'],array('sidebarid'=>$sidebarid)); ?>
      

      This way it passes the sidebarid var to the final widget form.

      The other change was in the includes/class-piklist-universal-widget.php :
      In the form function change this:

          piklist::render('shared/widget-select', array(
            'instance' => $instance
            ,'widgets' => $this->widgets
            ,'name' => $this->widget_core_name
            ,'widget_name' => $this->widget_name
            ,'class_name' => piklist::dashes($this->widget_core_name)
          ));
      

      to this:

          global $wp_registered_widgets, $wp_registered_sidebars;
          $sidebars_widgets = get_option('sidebars_widgets');
          foreach($sidebars_widgets AS $k=>$v) {
            if ($v[0]==$this->id) {
              $sidebarid=$k;
              break;
            }
          }    
          piklist::render('shared/widget-select', array(
            'instance' => $instance
            ,'sidebarid'=> $sidebarid
            ,'widgets' => $this->widgets
            ,'name' => $this->widget_core_name
            ,'widget_name' => $this->widget_name
            ,'class_name' => piklist::dashes($this->widget_core_name)
          ));
      

      All I did was add this before the render:

          global $wp_registered_widgets, $wp_registered_sidebars;
          $sidebars_widgets = get_option('sidebars_widgets');
          foreach($sidebars_widgets AS $k=>$v) {
            if ($v[0]==$this->id) {
              $sidebarid=$k;
              break;
            }
          }
      

      Which just gets the PROPER sidebar id.

      And added the ‘sidebarid’ argument to the render field.

      Little more work, but its working great.

      Now I have a toggle (via piklist meta box) in any page or post, which when turned on, turns the current page into a sidebar.

      Then in the widget form, it pulls the proper page from the sidebar id or description (testing both) and voila, I can load the necessary information for the widget form for that page/post in the form.

      Completely dynamic widgets and sidebars on a per page/post basis.

      Couldn’t have done it without you piklist.

      Marcus

    • #1320
      Kevin
      Keymaster

      Thanks for this Marcus. This is a reasonable addition to the system and we will make sure it gets in soon.

      Thanks

      Kevin

Viewing 2 reply threads
  • You must be logged in to reply to this topic.