Search code examples
phpcsswordpresssvgshortcode

Wordpress insert shortcode into menu item


I have such a problem. In functions.php I have such code:

function Svg_Path($attr) {
    $a = shortcode_atts(array(
        'path' => 'Some text'
    ), $attr);
    $svg = '<svg class="menu-item-icon"><use xlink:href=' . get_template_directory_uri(). '/img/svg/sprite.svg' . $a["path"] . '></use></svg>';
    return $svg;
}
add_shortcode( 'SvgPath', 'Svg_Path' );

Normally, I can use in pageBuilder this

<div>[SvgPath path='#logo__skype']</div>

And the result will be the displayed Skype Icon. But when I try to insert this shortcode into Appearence>Menu>Link text I get the shortcode as plain text

Where do I try to input my shortcode

So can you help me wwith this, so I can recieve skype logo from svg sprite in menu item


Solution

  • Try the following code. The code will parse your shortcode and show you the shortcode content. It'll only work on frontend.

    add_filter( 'the_title', function( $title, $item_id ) {
        if ( 'nav_menu_item' === get_post_type( $item_id ) ) {
            return do_shortcode( $title );
        }
        return $title;
    }, 10, 2 );