1

I am trying to target a widget with a specific class, and place something directly after the opening tag or before the widget title.

This:

<widget>
<h3>Title</h3>
<p>Some content goes here...</p>
</widget>

Becomes:

<widget>
<!-- Custom content or element goes here -->
<h3>Title</h3>
<p>Some content goes here...</p>
</widget>

I can't quite hook into it, I do not want to use css, this needs to be added with widget logic in order to be user-friendly.

0

You can use the dynamic_sidebar_params filter:

add_filter( 'dynamic_sidebar_params', 
    function( $params )
    {
       // target a sidebar id:
       if( isset( $params[0]['id'] ) && 'sidebar-1' === $params[0]['id']
       {    
           // target a widget name:
           if( isset( $params[0]['widget_name'] ) && 'Text' === $params[0]['widget_name'] )
           {
               // target a widget id:
               if( isset( $params[0]['widget_id'] ) && 'text-8' === $params[0]['widget_id'] )
               {
                   // Append text to existing 'before widget' markup code:
                   if( isset( $params[0]['before_widget'] ) )
                   { 
                       $params[0]['before_widget'] .= '<!-- Custom content -->';
                   }
               }
            }
         }
         return $params;
    } 
);

where the widget names are for example: Text, Categories, Calendar, ... .

You can then modify it to your needs, for example the 'sidebar-1', 'Text' and 'text-8' parts.

  • Thank you ever so much, that work like a charm! It's necessary to use class instead of id as I'd need multiple widgets with this purpose. Is it a simple case of changing widget_id to widget_class? Thank you again for such a fast and detailed response! – Paxjah Feb 28 '14 at 15:13
  • Ooh no worries! I removed the targeting of widget name and widget id :) – Paxjah Feb 28 '14 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.