I am working on a wordpress site that uses a plugin / sidebar widget. The issue I am experiencing is that the widget generates HTML that is not valid, an example of this is below...

<div>I am item 1</div>
<div>I am item 2</div>
<div>I am item 3</div>
<div>I am item 4</div>

From my understanding there is two things wrong with this code, the first being that it uses divs (block level element) inside of a Paragraph. The second being that there is an extra P tag that is not closed at the end.

I have spoken to the widget developers and, even though it is an expensive and paid plugin, are not in any rush to fix the issue.

I am going to attempt to use preg_replace to clean up the code, I was wondering if anyone had any examples or tutorials to doing this and targetting just the code within the widget container?

I realise this might be a question better placed on stackoverflow but though I would try my luck here first :)

  • 1
    You are better off editing the widget source to fix the problem before it occurs, than you are trying to regex a markup cleanup.
    – s_ha_dum
    Commented Nov 8, 2012 at 15:43
  • Are you asking how to perform the regex, or how to capture the widget output (upon which you can perform the regex)? Either way, I agree with s_ha_dum: fix the plugin if you can.
    – webaware
    Commented Jan 7, 2013 at 13:54

1 Answer 1


The cleanest way to do it without breaking forwards compatibility would be to create your own widget, extending the widget class you want to fix. Strip it back to the bare essentials of constructor and the widget method (in which you fix the HTML), then attach a function to widgets_init after the original widget which registers your widget and deregisters the original.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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