admin管理员组

文章数量:1289508

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.

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.

Share Improve this question edited Nov 20, 2016 at 13:43 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 20, 2016 at 12:10 Sahriar SaikatSahriar Saikat 1662 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

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

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

Result:

本文标签: phpHow to add Shortcode (font awesome) in widget title