Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save taricco/4d4dba99fb45dad251d62b08f272bb4b to your computer and use it in GitHub Desktop.

Select an option

Save taricco/4d4dba99fb45dad251d62b08f272bb4b to your computer and use it in GitHub Desktop.
// Add a GeneratePress Element at the TOP or BOTTOM of the Primary menu.
// generate_top_of_primary_menu
// generate_bottom_of_primary_menu
add_shortcode('top_of_primary_menu_hook', function($atts) {
ob_start();
$atts = shortcode_atts(array(
'hook_name' => 'top of menu'
), $atts, 'top_of_primary_menu_hook');
do_action($atts['hook_name']);
return ob_get_clean();
});
add_shortcode('bottom_of_primary_menu_hook', function($atts) {
ob_start();
$atts = shortcode_atts(array(
'hook_name' => 'bottom of menu'
), $atts, 'bottom_of_primary_menu_hook');
do_action($atts['hook_name']);
return ob_get_clean();
});
add_filter('wp_nav_menu_items', 'prefix_add_hooks_to_menu', 15, 2);
function prefix_add_hooks_to_menu($items, $args) {
if ($args->theme_location != 'primary') {
return $items;
}
// Add hook at the top of the menu
$items = '<li class="menu-item custom-menu-item-top">' . do_shortcode('[top_of_primary_menu_hook hook_name="generate_top_of_primary_menu"]') . '</li>' . $items;
// Add hook at the bottom of the menu
$items .= '<li class="menu-item custom-menu-item-bottom">' . do_shortcode('[bottom_of_primary_menu_hook hook_name="generate_bottom_of_primary_menu"]') . '</li>';
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment