I have the following code I'm trying to automatically add a menu to a theme via overriding a function. So I won't have to edit the theme file

However the menu is being outputted at the top of the page and on every page instead of only in its given location.

    if ( function_exists( 'wp_nav_menu' ) ){
        if (function_exists('add_theme_support')) {

            function register_cbx_menus() {
                        //'primary_navigation' => __( 'Main Menu'),
                        'legal_footer_navigation' => __( 'Legal Footer Menu'),
                        'echo'            => 0,
            add_action( 'init', 'register_cbx_menus' );

    //Function to override default footer credits
    if ( !function_exists('ci_theme_footer_credits') ) {  
       function ci_theme_footer_credits() {

         // Get blog/site name
         $cbx_blogame = get_bloginfo( 'name'); 

         //$cbx_footernav = ;

         //Output new footer content
         return '<div class="cbx-footer-nav">
            &copy; '.date('Y').'&nbsp;'. $cbx_blogname .'&nbsp;'.'All rights reserved. '.wp_nav_menu( array( 'theme_location' => 'legal_footer_navigation' ) ).'</div> '
         .'<div class="cbx-poweredby">Powered by <a href="http://www.example.com" target="_blank" title="example.com">example.com</a></div>';
       }    //End function

     } //End function

How can I modify the above code to properly add the menu to a theme via the fucntion.php file without having to edit the theme file to add

wp_nav_menu( array( 'theme_location' => 'legal_footer_navigation' )); 
  • I'm curious, why can't you edit the theme file or create a child theme? May 13 '13 at 17:55
  • because I would have edit 30+ themes
    – Oudin
    May 14 '13 at 3:33

I think your current call to wp_nav_menu() may be echoing the menu when the code is seen and not returning it in the function.

Try this call:

wp_nav_menu( array(
    'theme_location' => 'legal_footer_navigation',
    'echo'           => false,
) );

If that works, you can also probably remove the echo parameter from the register_nav_menus() call.

  • Thanks it works the only issue is using return makes the menu appear in front of everything else as oppose to exactly where I want it
    – Oudin
    Jun 5 '13 at 23:23
  • You may be able to use the CSS z-index property to layer it behind other elements. Or you could get more help by asking a new question with a link to your test page. w3.org/TR/CSS21/visuren.html#propdef-z-index Jun 5 '13 at 23:29

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.