How to add Shortcode (font awesome) in widget title?

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

[icon]cog[icon]

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

[icon]cog[/icon]

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"]

Leave a Comment