PHP code to add items to ClassicPress and WordPress menus.

PHP code to add items to ClassicPress and WordPress menus.

This PHP code will dynamically add items to a Wordpress or ClassicPress menu. In this example I am adding 4 social media links to the menu I have created called “top”. This menu is the Primary Menu that I use on burnett.tech.

I find it easier to just use the wp_nav_menu_items WordPress hook for adding menu items instead of just creating a static menu.

add_filter('wp_nav_menu_items','add_navs_to_menu', 10, 2);
function add_navs_to_menu( $items, $args ) {
    if( $args->theme_location == 'top' )
    {
        $items  .= '<li class="menu-item"><a target="_new" href="https://twitter.com/burnett_tech"><i class="fab fa-twitter"></i></a></li>';
        $items  .= '<li class="menu-item"><a target="_new" href="https://www.linkedin.com/company/burnett-tech"><i class="fab fa-linkedin"></i></a></li>';
        $items  .= '<li class="menu-item"><a target="_new" href="https://www.github.com/burnett-tech"><i class="fab fa-github"></i></a></li>';
        $items  .= '<li class="menu-item"><a target="_new" href="https://feeds.feedburner.com/burnett-tech"><i class="fas fa-rss"></i></a></li>';
    }

    return $items;
}

Leave a Reply