retesere20

wp-navigation-menu-hooks

Nov 28th, 2019
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //register desired widget names
  3. $GLOBALS['MyWidgetss'] = array (
  4. 'Myy-area-1___LEFT','Slideshow-area-4', 'area-Custom4',
  5. );
  6. // registered menus
  7. $GLOBALS['REGISTERED_MENUS'] =array('main-top', 'main-nav','footer-nav' );
  8. $GLOBALS['REGISTERED_MENU_CLASSES_ul'] =array('main-top'=>'nav navbar-nav sf-menu clearfix' );
  9. $GLOBALS['REGISTERED_MENU_CLASSES_li'] =array();
  10. $GLOBALS['REGISTERED_MENU_CLASSES_li_CHILDED'] =array('main-top'=>'sub-menu sub-menu-1' );
  11. $GLOBALS['REGISTERED_MENU_CLASSES_a'] =array('main-top'=>'nav navbar-nav sf-menu clearfix' );
  12. //$GLOBALS['REGISTERED_MENUS_ADD_EXTRA_li'] ='<li class="emptyItem><a class=""></a></li>';
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. // ========================================= MENUS ============================ //
  24. // https://developer.wordpress.org/reference/functions/wp_nav_menu/
  25. //add some custom-menus
  26. add_action('init', 'my_menu_registerss24');
  27. function my_menu_registerss24() {
  28. if (!empty($GLOBALS['REGISTERED_MENUS'])){
  29. foreach($GLOBALS['REGISTERED_MENUS'] as $name=>$value) {register_nav_menu($value,'my '.$value);}
  30. }
  31. //register_nav_menus(array( ));
  32. }
  33. // https://developer.wordpress.org/reference/functions/wp_nav_menu/
  34. // output is done in template, i.e. ----> wp_nav_menu( array( 'theme_location' => 'my-main-top', 'menu_id'=> 'primary-menu'));
  35. // ================ this is default output ============//
  36. // <ul>
  37. // <li><a>....</a></li>
  38. // </ul>
  39. // to modify the arguments, use the below "wp_nav_menu_args" function
  40. // ------------<ul> class
  41. add_filter( 'wp_nav_menu_args', 'modify_nav_menu_args' , 9);
  42. function modify_nav_menu_args( $args ) {
  43. if(!empty($GLOBALS['REGISTERED_MENU_CLASSES_li_CHILDED']) && !empty($GLOBALS['REGISTERED_MENU_CLASSES_ul']) ){
  44. foreach($GLOBALS['REGISTERED_MENU_CLASSES_ul'] as $name=>$value){
  45. if($args['theme_location'] == $name || $args['menu_id'] == $name) {
  46. $args['menu_class'] .= ' '.$value;
  47. }
  48. }
  49. }
  50. return $args;
  51. }
  52.  
  53. // ------------<li> class
  54. add_filter('nav_menu_css_class', 'auto_custom_type_class', 9, 4 );
  55. function auto_custom_type_class($classes, $item, $args=false, $depth=false ) {
  56. //if ($args->menu == 'main-top'){$classes[] = "New_Class"; }
  57. if(!empty($GLOBALS['REGISTERED_MENU_CLASSES_li'])){
  58. foreach($GLOBALS['REGISTERED_MENU_CLASSES_li'] as $name=>$value){
  59. if($args !==false && ($args->theme_location == $name|| $args->menu_id == $name) ) {$classes[] = $value; }
  60. }
  61. }
  62. if(!empty($GLOBALS['REGISTERED_MENU_CLASSES_li_CHILDED'])){
  63. foreach($GLOBALS['REGISTERED_MENU_CLASSES_li_CHILDED'] as $name=>$value){
  64. if($args !==false && ($args->theme_location == $name|| $args->menu_id == $name) && in_array('menu-item-has-children',$classes)) { $classes[] = $value; }
  65. }
  66. }
  67. return $classes;
  68. }
  69. // ------------<a> class
  70. add_filter( 'nav_menu_link_attributes', 'add_menu_atts', 8, 3 );
  71. function add_menu_atts( $atts, $item, $args=false ) {
  72. if(!empty($GLOBALS['REGISTERED_MENU_CLASSES_a'])){
  73. foreach($GLOBALS['REGISTERED_MENU_CLASSES_a'] as $name=>$value){
  74. if($args !==false && $args->theme_location == $name) { $classes[] = $value; }
  75. }
  76. }
  77. $atts['class'] = (isset($atts['class'])? $atts['class']:''). ' '.$item->type.'-'.$item->ID;
  78. return $atts;
  79. }
  80.  
  81.  
  82. //add LI item to menu
  83. add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link', 9 );
  84. function my_nav_menu_profile_link($menu) {
  85. return $menu . (!empty($GLOBALS['REGISTERED_MENUS_ADD_EXTRA_li']) ? $GLOBALS['REGISTERED_MENUS_ADD_EXTRA_li'] : '') ;
  86. }
  87. // ==================================== END MENUS =========================//
RAW Paste Data