1

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

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

  • 1
    I was just writing an answer, so I just append it to your answer, since you posted it before me ;-) – birgire Nov 20 '16 at 13:47
  • @birgire I especially agree with your fourth bullet. – cjbj Nov 20 '16 at 13:52
  • dear god... give me wisdom and focus :D thank you. it was the backslash I missed. I was too focused on code that didn't notice it. – Sahriar Saikat Nov 20 '16 at 15:08
  • @birgire How would you make it to be like [fa icon="cog"] – Pete Feb 3 at 4:36
  • @Pete I would suggest looking first at the shortcode API. Hope that helps. – birgire Feb 3 at 19:59

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.