Area as widget area unless menu assigned problem


I think I'm confusing the forest with the trees, somewhat. Might be because I figured out a way how to work on a child theme in a cloud ide, resulting in forgetting dinner.

I've been able to create widget areas, as well as overwrite the default footer, thanks to the docs. But I'm not sure how to approach this bit.

I want to create an area which can function as a widget area provided no menu is assigned to that area. Does that make sense?

Do I create the area as a widget area first, or do I use setup_document? I guess I'm a little stuck in an old line of thinking, I suppose.

// 9 Adding a Footer Middle Area for all pages

// 9.1 Register a Footer Middle Area for all pages

add_action( 'widgets_init', 'fmid_widget_area' );

function fmid_widget_area() {

    beans_register_widget_area( array(
        'name' => 'Footer Middle',
        'id' => 'fmid',
        'beans_type' => 'grid'
    ) );

}

// 9.2 Display the Footer Middle Area in the front-end

add_action( 'beans_footer_before_markup', 'fmid_widget_area' );

function fmid_footer_widget_area() {

 ?>
  <div class="tm-mega-footer uk-block">
   <div class="uk-container uk-container-center">
      <?php 
            wp_nav_menu( array( 
                'menu' => 'Footer Middle Menu',
                'menu_class' => 'uk-subnav uk-subnav-line',
                'container' => 'div',
                'container_class' => 'uk-align-medium-right',.
                'theme_location' => 'fmid-menu',
                'beans_type' => 'navbar'
            ) ); 
            ?>
    </div>
  </div>
  <?php

}

// 9.3 Register the Footer Middle Menu

// New Footer Menu
function register_fmid_menu() {

    register_nav_menu( 'fmid-menu',__( 'Footer Middle Menu' ) );

}

// 9.4 Initialize Footer Middle Menu

add_action( 'init', 'register_fmid_menu' );

// 9.5 Display the Footer Middle Menu in the front end? put it in its section?

Any pointers? Or should I just forget this idea, and figure out how to put a section between a Footer Top Widget area and the regular Footer area?


Hi J.C.

If I understand will you want to add a custom menu before the footer, right? If yes, then your code look prefectly fine but you can remove the widget area register part as you don't need a widget area for it.

So your register_fmid_menu() function register the menu location which is correct, then your fmid_footer_widget_area() function adds the menu before the footer which is also correct. I would just advise to change the function name fmid_footer_widget_area to fmid_footer_menu for example (don't forget to change the action second argument instance too if you change the function name).

So I think the confusion was that adding a custom menu does not necessarly need a custom widget area.

Then you can indeed add an extra widget area on top of it if you want to indeed πŸ™‚

Happy coding,


Hmm. I suppose that is indeed where my thinking took a wrong turn. Still struggling with the notion that you can use beans to effectively structure a page as well as the theme itself from the ground up.

I'm basically doing experiments as learning steps, with the idea to finally figure out how to create page templates (such as https://cldup.com/WJth2-HK3Z.png).

A question though. If I use fmid_footer_menu as an approach, does it matter in what order I put the functions for these things in the functons.php? Also, is adding an extra widget area not going to cause a fatal error if a menu were active?


Still confused. It's not working. You suggest to change the function name fmid_footer_widget_area to fmid_footer_menu.

First I use add_action to create a widget area called fmid_widget_area.

add_action( 'widgets_init', 'fmid_widget_area' );

function fmid_widget_area() {

    beans_register_widget_area( array(
        'name' => 'Footer Middle',
        'id' => 'fmid',
        'beans_type' => 'grid'
    ) );

}

I then use add_action to display said "widget" area in the frontend. Instead of putting the - for a widget area typical <?php echo beans_widget_area( 'fmid' ) in there, I tell it to put a menu there. BUT as a widget area? Either way, I change the function name from fmid_footer_widget_area to fmid_footer_menu.

add_action( 'beans_footer_before_markup', 'fmid_footer_menu' );

function fmid_footer_menu() {

    ?>
    <div class="tm-mega-footer uk-block">
        <div class="uk-container uk-container-center">
            <?php 
            wp_nav_menu( array( 
                'menu' => 'Footer Middle Menu',
                'menu_class' => 'uk-subnav uk-subnav-line',
                'container' => 'div',
                'container_class' => 'uk-align-medium-right',
                'theme_location' => 'fmid-menu',
                'beans_type' => 'navbar'
            ) ); 
            ?>
        </div>
    </div>
    <?php

}

At this point, with wordpress knowing the is a widget area and a footer menu function I tell it to first register and then initialize the actual menu.

// New Footer Menu
function register_fmid_menu() {

    register_nav_menu( 'fmid-menu',__( 'Footer Middle Menu' ) );

}

add_action( 'init', 'register_fmid_menu' );

After that, this is where I display the Footer Middle widget area to the front-end. Which is where I tell it where to show any widgets put there with <?php echo beans_widget_area( 'fmid' ); ?>.

add_action( 'beans_footer_before_markup', 'fmid_widget_area' );

function fmid_footer_widget_area() {

    ?>
 <div class="tm-mega-footer uk-block">
   <div class="uk-container uk-container-center">
      <?php echo beans_widget_area( 'fmid' ); ?>
    </div>
  </div>
  <?php

}

There has to be a better way to approach this. At least more elegant. Anyway πŸ˜›

Within WP, the widget area does indeed show up, ready for potential use. I can also assign a menu to the menu area. On page however, no menu is displayed. If I add a widget to the widget area, regardless of whether a menu is assigned, the widget does display.

So widget yes, menu no. Weird.


Well, frack me sideways, owned by a typo. I've edited the code in the previous message to reflect what does work! πŸ™‚ Edit: except that now other widget areas custom created don't show assigned widgets when a menu is active in this area. Hm.


Alright, this was fun. It appears something - I have no idea what - breaks down when doing the above. If you create a footer menu, the only widget areas which then still function are those created with the default beans_child functions like the Hero widget area.

Sidebar, footer stuff, if you have a menu added / initialized, it won't display widgets.

add_action( 'widgets_init', 'beans_child_widgets_init' );

function beans_child_widgets_init() {

    beans_register_widget_area( array(
        'name' => 'Hero',
        'id' => 'hero',
        'description' => 'Widgets in this area will be shown in the hero section as a grid.',
        'beans_type' => 'grid'
    ) );

}

add_action( 'beans_main_grid_before_markup', 'beans_child_hero_widget_area' );

function beans_child_hero_widget_area() {

 echo beans_widget_area( 'hero' );

}

This works.

add_action( 'widgets_init', 'cta_widget_area' );

function cta_widget_area() {

    beans_register_widget_area( array(
        'name' => 'CTA',
        'id' => 'cta',
        'beans_type' => 'grid'
    ) );

}

add_action( 'beans_footer_before_markup', 'cta_widget_area' );

function cta_footer_widget_area() {

 ?>
  <div class="tm-mega-footer uk-block">
   <div class="uk-container uk-container-center">
      <?php echo beans_widget_area( 'cta' ); ?>
   </div>
  </div>
  <?php

}

This doesn't.



Hey J.C.

This issue in the snippet in this reply is that your function to display the widget is called cta_footer_widget_area but your second argument in the add_action is set to cta_widget_area.

This is why nothing was happening as the add_action callback needs to match the function name πŸ˜‰

Happy coding,

Write a reply

Login or register to write a reply, it's free!