I’ve been trying to turn the following line of PHP into a shortcode in my functions.php so that I can add it to a WordPress page.

<?php wp_nav_menu( array( 'theme_location' => 'header' ) ); ?>

After some research I found the code below for creating shortcodes, but I’m not sure how to add the line of PHP above to it to it, or even if it is the correct way to do this.

function menu_shortcode($atts, $content = null){
    return '<nav class="main-menu-header">' . do_shortcode($content) . '</nav>';

add_shortcode('nav', 'menu_shortcode');

I initially assumed I was to add the code between the brackets of do_shortcode instead of having $content, but this gave me an error.

Any help would be appreciated very much.

Read more here: How to make a PHP function into a short code


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: