I have created a sidebar:

function my_sidebar() {

            'name'          => 'Blog Sidebar',
            'id'            => 'blog-sidebar',
            'description'   => 'Sidebar For Blog Page',
            'before_widget' => '<div class="widget">',
            'after_widget'  => '</div>',
            'before_title'  => '<h5 class="widget-title">',
            'after_title'   => '</h5>'

add_action( 'widgets_init', 'my_sidebar');

I just added the "recent posts" widget and want to add a class to the <ul> tag but I can't seem to figure out how to do it in a simple way. I just want the <ul> tag to change to <ul class="link-list">


The quickest and practical solution for you would be to add your custom class to the before_widget wrapper:

'before_widget' => '<div class="widget ul-link-list">'

Then use CSS rules accordingly:

.ul-link-list ul {
  /* Some CSS rules */
| improve this answer | |
  • Thanks for your comment but that results in <div class="widget ul-link-list"> which is not what I want. I want to add a class to my ul list, not the div with class of 'widget'. – user8463989 May 6 '18 at 11:30
  • 1
    As i stated, this is the quickest workaround for you. Otherwhise you'll have to use javascript to dynamically add the CSS class to your ul element, or use some other wp hooks along with ob_start() and ob_get_clean() functions (you will probably also need regexp for this) – Eduardo Escobar May 6 '18 at 11:35

It's not possible to modify markup normally via actions/filters because markup is hardcoded in Wordpress \wp-includes\theme-compat\sidebar.php enter image description here

So it's possible to change only via js, or php html output parsing

| improve this answer | |
  • The files in theme-compat/ are not used normally. You can completely ignore them, and yes, change the markup in your own sidebars. – fuxia Jun 1 at 16:06

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.