Check if add_menu_page exists or not

You can use the fourth parameter of add_menu_page(), the my_unique_slug, to check if the page exists: if ( empty ( $GLOBALS[‘admin_page_hooks’][‘my_unique_slug’] ) ) add_menu_page( ‘Page Title’, ‘Top Menu Title’, ‘manage_options’, ‘my_unique_slug’, ‘my_magic_function’ ); $GLOBALS[‘admin_page_hooks’] is the list of registered pages.

How to add sub-menu to a menu generated by wp_nav_menu by using plugin

You can modify your menu by using walker. include(‘subMenu.php’); $menu = wp_nav_menu( array(‘menu’ => ‘YOUR-MENU-NAME’,’menu_class’ => ‘megamenu’,’walker’ => new subMenu)); create a file subMenu.php in theme folder add below code. <?php class subMenu extends Walker_Nav_Menu { function end_el(&$output, $item, $depth=0, $args=array()) { if( ‘Item 3’ == $item->title ){ $output .= ‘<ul class=”sub-menu”> <li class=”menu-item” id=”menu-item-48″><a … Read more

Get page IDs from nav items

Menu items are stored in the posts table with a post_type of nav_menu_item. So, what you are returning is the ID of the menu item itself, not what it points to. The page/post ID that the menu item refers to is stored in the postmeta table, with a post_id that matches the menu item ID … Read more

Convert output of nav_menu items into a tree-like multidimensional array

The problem of building a tree from a flat array has been solved here with this, slightly modified, recursive solution: /** * Modification of “Build a tree from a flat array in PHP” * * Authors: @DSkinner, @ImmortalFirefly and @SteveEdson * * @link https://stackoverflow.com/a/28429487/2078474 */ function buildTree( array &$elements, $parentId = 0 ) { $branch … Read more

wp_nav_menu: show menu only if one exists, otherwise show nothing

Use has_nav_menu(), and test for theme_location, rather than menu_id: <?php if ( has_nav_menu( $theme_location ) ) { // User has assigned menu to this location; // output it wp_nav_menu( array( ‘theme_location’ => $theme_location, ‘menu_class’ => ‘nav’, ‘container’ => ” ) ); } ?> You can output alternate content, by adding an else clause. EDIT You … Read more

Removing container from wp_nav_menu not working

[SOLVED] IT DOES NOT WORK when you are referring to an inexisting location. e.g. when you copied the code from somewhere else, or you haven’t created your menu or location yet in the dasboard. e.g. remove “, ‘theme_location’ => ‘primary’” from the following code: wp_nav_menu( array( ‘container’=> false, ‘menu_class’=> false, ‘menu_id’=> ‘ia_toplevel’, ‘theme_location’ => ‘primary’ … Read more

Adding first / last CSS classes to menus

A better and simpler approach: function add_first_and_last($items) { $items[1]->classes[] = ‘first-menu-item’; $items[count($items)]->classes[] = ‘last-menu-item’; return $items; } add_filter(‘wp_nav_menu_objects’, ‘add_first_and_last’);

Split up wp_nav_menu with custom walker

Using a custom Walker, the start_el() method has access to $depth param: when it is 0 the elemnt is a top one, and we can use this info to maintain an internal counter. When the counter reach a limit, we can use DOMDocument to get from full HTML output just the last element added, wrap … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)