Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

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

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 :)

share|improve this question
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 Nov 8 '12 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 Jan 7 '13 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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