Adding html to your WordPress widget title

I was recently working on a custom WordPress widget where I wanted to include an image in the widget title. For security reasons, WordPress sanitizes the html so it was not working for me. I searched and searched on the internets but ran into solutions that were bulky and didn’t work for me. After looking into the text widget source code which allows html, I found this useful filter:

apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );

I adapted it to my WordPress plugin for the widget like so:

$instance['title'] = '<img src="https://www.some_website.com/images/logo.png">My Logo';
apply_filters( 'widget_text', $instance['title'] );

And that does it!

Published by

Allen Liu

Building websites is a passion of mine. During the course of my work, I have created some snippets of code that I hope will prove useful to other developers.

Leave a Reply

Your email address will not be published. Required fields are marked *