1

I'm trying to get the sidebar HTML output to filter the <img> tags and change its src attributes, but I couldn't so far.

I tried with this code inside of dynamic_sidebar filter hook, but didn't work:

ob_start();
dynamic_sidebar( $sidebar['id'] );
$sidebar_html = ob_get_contents();
ob_end_clean();

Does anyone knows how to do it? Thanks.

  • You can add a text widget, this reads and place a html content, for example <img src=... en so on. – Fernando Baltazar Mar 2 '17 at 3:36
  • That's not what I want. For example, some themes have those Latest Posts Widgets which displays the thumbnails with its respective post. What I want is to get that HTML content from that Widget. Am I clear? Thanks. – Roel Magdaleno Mar 2 '17 at 3:44
  • What's the original problem that requires you to filter the img tags? Could you not filter the widget itself? Some more context would be very helpful, especially as you've opted for the bruteforce approach – Tom J Nowell Mar 2 '17 at 4:21
  • Some part of the Widget output simply cannot be modified using filters. You can obviously do it with output buffering (the way you tried), but that's not recommended. Instead you should extend that particular Widget class and write your own custom widget() function within that class. – Fayaz Mar 2 '17 at 5:27
  • I'm creating a Lazy Load plugin which requires Lazy Load the thumbnails of that kind of Widgets I mentioned in my previous comment and, I need to filter the <img> tags to add them a lazy load attribute. @tom-j-nowell – Roel Magdaleno Mar 2 '17 at 18:17

Your Answer

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

Browse other questions tagged or ask your own question.