• Hi,
    i Have been trying to change the before_widget string for the first instance, as well well as the after_widget string for the last instance in order to enclose a certain set of widgets in a div in order to make them tabs.
    First tried with jquery, altering the DOM to recieve the desired result, but wordpress seems to prevent that from working correctly, as my opened div always gets closed directly.
    So i thought i try it with filters to hook into the generation of the widget divs and change the wrapping strings to what i need, until now to no avail. anybody has a thought on this ? (code below, is run in widget function):

    //COunting Instances of widget
    $num = 0;
    foreach(wp_get_sidebars_widgets() as $pos => $id) {
    	if($pos == $args['id']) {
    	foreach($id as $k => $v) {
    	if(strstr($v, 'evoffice_widget')) $num++;
    }}}
    //initalize globals for Original sidebar strings
    $OrigBeforeWidget = "";
    $OrigAfterWidget= "";
    
    //Manipulate before_widget on first occurence of widget
    if($instance['Tabs'] == 'true' && $instance['OfficeIndex'] == 1) {
    	add_filter('dynamic_sidebar_params', 'Start_Office_Block'); 
    
    function Start_Office_Block($params){
       global $OrigBeforeWidget;
       $OrigBeforeWidget = $params[0]['before_widget'];
    //Add the opening div to the before_widget
            $params[0]['before_widget'] = '<div id="OfficeTabs">'.$params[0]['before_widget'];
    
        return $params;
    }
    //Declare only once (with first instance) for resetting before and after widget strings - function call is made after after_widget at the bottom
    
    function ResetWidgetsBeforeAfter($params){
      global $OrigBeforeWidget;
      global $OrigAfterWidget;
            $params[0]['before_widget'] = $OrigBeforeWidget;
    	$params[0]['after_widget'] = $OrigAfterWidget;
    
        return $params;
    } 
    
    }
    
    //run if is last instance ($num is number of widgets of this type)
    if($instance['Tabs'] == 'true' && $instance['OfficeIndex'] == $num) {
    	add_filter('dynamic_sidebar_params', 'End_Office_Block');
    function End_Office_Block($params){
      global $OrigAfterWidget;
    	$OrigAfterWidget = $params[0]['after_widget'];
            // its your widget so you add  your classes
            $params[0]['after_widget'] = $params[0]['after_widget'].'</div>';
    
        return $params;
    }
    }
  • The topic ‘Manipulating before_widget and after_widget’ is closed to new replies.