I am trying to add custom links next/before my built in menu, specifically for main_menu. I have placed this in my functions.php and have given list-items in nav-menu-after.php and also in the before file but it's not working.

add_filter( 'wp_nav_menu_items',do_wp_nav_menu_items );
function do_wp_nav_menu_items( $menu, $args = array() ) {
    if( file_exists( TEMPLATEPATH . '/nav-menu-before.php' ) && $args->theme_location == 'main_menu' ) {
        @include TEMPLATEPATH . '/nav-menu-before.php';
        $nav_menu_before = ob_get_clean();

    if( file_exists( TEMPLATEPATH . '/nav-menu-after.php'  ) && $args->theme_location == 'main_menu' ) {
        @include TEMPLATEPATH . '/nav-menu-after.php';
        $nav_menu_after = ob_get_clean();

    return $nav_menu_before . $menu . $nav_menu_after;

If I delete this part:

&& $args->theme_location == 'main_menu' 

It works well but the custom static links appear in the whole menu I have (footer menu, primary menu). Not only main_menu.

1 Answer 1


If you will not pass that how many arguments you need in your callback function then add_filter will provide you only one and first argument.

Because default arguments for add_filter are

add_filter ( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 );

$accepted_args is 1 if you do not pass it.

Thus $args is always blank array in your callback function.




add_filter('wp_nav_menu_items', 'do_wp_nav_menu_items', 10, 2);
  • Great, Sumit! It now worked well. Anyway I would like to ask, will I need to posted such these question based on the field I am asking? I meant I have posted SAME question in stackoverflow but it got no single reply for 3-4 days, also several question based on wordpress. I can conclude then stackoverflow for common programming question, right? Like PHP or Java stuffs. Again thanks @Sumit !
    – gijoe
    Jun 13, 2016 at 23:55
  • Yw :) Yes on Stackoverflow you can ask any programming language/CMS question. But if specific site exist then better to ask there like WordPress question on this site. But only core WordPress see what you can ask here. The answer depends on quality of question and asker responsiveness. I can see you never closed a question on stackoverflow! You should always close question by accepting answer if it solves your issue.
    – Sumit
    Jun 14, 2016 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.