I want to show font awesome icon (installed) on the left side of the WordPress widget title. I found this shortcode that should do the work.

add_filter( 'widget_title', 'do_shortcode' );

add_shortcode( 'icon', 'shortcode_fa' );
function shortcode_fa($attr, $content ) {
return '<i class="fa fa-'. $content . '"></i>';

After adding this in functions.php, I should be able to add a gear icon in the widget title with below code from Appearence>Widget


But it is not working.

2 Answers 2


I checked your code in my install. It works, except that you made a typo (missing backslash):


Few notes:

  • You must make sure to enqueue the Font Awsesome stylesheet.

  • You must close the shortcode, like: [icon]cog[/icon]

  • Remember to escape the class name with esc_attr().

  • Another shortcode idea: [fa icon="cog"]

  • 1
    I was just writing an answer, so I just append it to your answer, since you posted it before me ;-)
    – birgire
    Nov 20, 2016 at 13:47
  • @birgire I especially agree with your fourth bullet.
    – cjbj
    Nov 20, 2016 at 13:52
  • @birgire How would you make it to be like [fa icon="cog"]
    – Pete
    Feb 3, 2019 at 4:36
  • @Pete I would suggest looking first at the shortcode API. Hope that helps.
    – birgire
    Feb 3, 2019 at 19:59

Full working code here!

Add this to functions.php:

add_filter( 'widget_title', 'do_shortcode' );
add_shortcode( 'fa', 'shortcode_fa' );
function shortcode_fa($attr, $content ) {
    $icon = $attr['icon'];
    return "<i class='fa fa-$icon'></i>";

Add shortcode to widget title:

[fa icon=copyright] Widget title text


enter image description here

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.