Advertisement
alamin_89

Nav Walker me 1st

Dec 30th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.17 KB | None | 0 0
  1. <?php
  2.  
  3. // add custom menu fields to menu
  4. add_filter('wp_setup_nav_menu_item', 'portomy_add_custom_nav_fields');
  5.  
  6. function portomy_add_custom_nav_fields( $menu_item ){
  7.     $menu_item->icon = get_post_meta( $menu_item->ID, '_menu_item_icon', true );
  8.     return $menu_item;
  9. }
  10.  
  11. // save menu custom fields
  12. add_action('wp_update_nav_menu_item', 'portomy_update_custom_nav_fields', 10, 3);
  13.  
  14. function portomy_update_custom_nav_fields( $menu_id, $menu_item_db_id, $args ){
  15.     $check = array( 'icon' );
  16.     foreach ( $check as $key ){
  17.         if(!isset($_POST['menu-item-'.$key][$menu_item_db_id])){
  18.             if (!isset($args['menu-item'.$key]))
  19.                 $value = "";
  20.             else
  21.                 $value = $args['menu-item-'.$key];
  22.         } else {
  23.             $value = $_POST['menu-item-'.$key][$menu_item_db_id];
  24.         }
  25.        
  26.         if($value){
  27.             update_post_meta( $menu_item_db_id, '_menu_item_'.$key, $value );
  28.         } else {
  29.             delete_post_meta( $menu_item_db_id, '_menu_item_'.$key );
  30.         }
  31.     }
  32. }
  33.  
  34. // edit menu walker
  35. add_filter( 'wp_edit_nav_menu_walker', 'portomy_menu_edit_walker', 10, 2 );
  36.  
  37. function portomy_menu_edit_walker($walker = '', $menu_id = ''){
  38.     return 'Portomy_Walker_Nav_Menu_Edit';
  39. }
  40.  
  41. // Create HTML list of nav menu input items.
  42. // Extend from Walker_Nav_Menu class
  43. class Portomy_Walker_Nav_Menu_Edit extends Walker_Nav_Menu {
  44.    
  45.     function start_lvl( &$output, $depth = 0, $args = array() ){
  46.        
  47.     }
  48.    
  49.     function end_lvl( &$output, $depth = 0, $args = array() ){
  50.        
  51.     }
  52.    
  53.     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){
  54.         global $_wp_nav_menu_max_depth;
  55.        
  56.         $_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
  57.        
  58.         $indent = ( $depth ) ? str_repeat( "\t", $depth) : '';
  59.        
  60.         $item_id = esc_attr( $item->ID );
  61.         $removed_args = array(
  62.             'action',
  63.             'customerlink-tab',
  64.             'edit-menu-item',
  65.             'menu-item',
  66.             'page-tab',
  67.             '_wpnonce',
  68.         );
  69.         ob_start();
  70.         $orginal_title = '';
  71.         if ( 'taxonomy' == $item->type ){
  72.             $orginal_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
  73.             if ( is_wp_error( $orginal_title ) )
  74.                 $orginal_title = false;
  75.         } elseif ( 'post_type' == $item->type ) {
  76.             $original_object = get_post( $item->object_id );
  77.             $original_title = $original_object->post_title;
  78.         }
  79.        
  80.         $classes = array(
  81.             'menu-item menu-item-depth-' . $depth,
  82.             'menu-item-' . esc_attr( $item->object ),
  83.             'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
  84.         );
  85.        
  86.         $title = $item->title;
  87.        
  88.         if ( ! empty( $item->_invalid ) ) {
  89.             $classes[] = 'menu-item-invalid';
  90.             /* translators: %s: title of menu item which is invalid */
  91.             $title = sprintf( '%s (Invalid)', $item->title );
  92.         } elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
  93.             $classes[] = 'pending';
  94.             /* translators: %s: title of menu item in draft status */
  95.             $title = sprintf( '%s (Pending)', $item->title );
  96.         }
  97.        
  98.         $title = empty( $item->label ) ? $title : $item->label;
  99.         ?>
  100.             <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
  101.                 <dl class="menu-item-bar">
  102.                     <dt class="menu-item-handle">
  103.                         <span class="item-title"><?php echo esc_html( $title ); ?></span>
  104.                         <span class="item-controls">
  105.                             <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
  106.                             <span class="item-order hide-if-js">
  107.                                 <a href="<?php
  108.                                     echo wp_nonce_url(
  109.                                         esc_url( add_query_arg(
  110.                                             array(
  111.                                                 'action' => 'move-up-menu-item',
  112.                                                 'menu-item' => $item_id,
  113.                                             ),
  114.                                             esc_url( remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) ) )
  115.                                         ) ),
  116.                                         'move-menu_item'
  117.                                     );
  118.                                     ?>" class="item-move-up"><abbr title="Move up">&#8593;</abbr></a>
  119.                                 |
  120.                                 <a href="<?php
  121.                                     echo wp_nonce_url(
  122.                                         esc_url( add_query_arg(
  123.                                             array(
  124.                                                 'action' => 'move-down-menu-item',
  125.                                                 'menu-item' => $item_id,
  126.                                             ),
  127.                                             esc_url( remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) ) )
  128.                                         ) ),
  129.                                         'move-menu_item'
  130.                                     );
  131.                                     ?>" class="item-move-down"><abbr title="Move down">&#8595;</abbr></a>
  132.                             </span>
  133.                             <a class="item-edit" id="edit-<?php echo $item_id; ?>" title="Edit Menu Item" href="<?php
  134.                                 echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] )
  135.                                     ? admin_url( 'nav-menus.php' )
  136.                                     : esc_url( add_query_arg( 'edit-menu-item', $item_id,
  137.                                         esc_url( remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) ) ) );
  138.                                 ?>"><?php echo 'Edit Menu Item'; ?></a>
  139.                         </span>
  140.                     </dt>
  141.                 </dl>
  142.                 <div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
  143.                     <?php if( 'custom' == $item->type ) : ?>
  144.                     <p class="description description-wide">
  145.                         <label for="edit-menu-item-url-<?php echo $item_id; ?>">
  146.                             <?php echo 'URL'; ?><br />
  147.                             <input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url"
  148.                                 <?php if (esc_attr( $item->url )) : ?>
  149.                                    name="menu-item-url[<?php echo $item_id; ?>]"
  150.                                 <?php endif; ?>
  151.                                    data-name="menu-item-url[<?php echo $item_id; ?>]"
  152.                                    value="<?php echo esc_attr( $item->url ); ?>" />
  153.                         </label>
  154.                     </p>
  155.                         <?php endif; ?>
  156.                     <p class="description description-wide">
  157.                         <label for="edit-menu-item-title-<?php echo $item_id; ?>">
  158.                             <?php echo 'Navigation Label'; ?><br />
  159.                             <input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title"
  160.                                 <?php if (esc_attr( $item->title )) : ?>
  161.                                    name="menu-item-title[<?php echo $item_id; ?>]"
  162.                                 <?php endif; ?>
  163.                                    data-name="menu-item-title[<?php echo $item_id; ?>]"
  164.                                    value="<?php echo esc_attr( $item->title ); ?>" />
  165.                         </label>
  166.                     </p>
  167.                     <p class="description">
  168.                         <label for="edit-menu-item-target-<?php echo $item_id; ?>">
  169.                             <input type="checkbox" id="edit-menu-item-target-<?php echo $item_id; ?>" value="_blank"
  170.                                 <?php if ($item->target == '_blank') : ?>
  171.                                    name="menu-item-target[<?php echo $item_id; ?>]"
  172.                                 <?php endif; ?>
  173.                                    data-name="menu-item-target[<?php echo $item_id; ?>]"
  174.                                 <?php checked( $item->target, '_blank' ); ?> />
  175.                             <?php echo 'Open link in a new window/tab'; ?>
  176.                         </label>
  177.                     </p>
  178.                     <?php
  179.                     /* New fields insertion starts here */
  180.                     ?>
  181.                     <p class="description description-wide">
  182.                         <label for="edit-menu-item-icon-<?php echo $item_id; ?>">
  183.                             <?php echo 'Font Awesome Icon Class'; ?><br />
  184.                             <input type="text" id="edit-menu-item-icon-<?php echo $item_id; ?>" class="widefat code edit-menu-item-icon"
  185.                                 <?php if (esc_attr( $item->icon )) : ?>
  186.                                     name="menu-item-icon[<?php echo $item_id; ?>]"
  187.                                 <?php endif; ?>
  188.                                    data-name="menu-item-icon[<?php echo $item_id; ?>]"
  189.                                    value="<?php echo esc_attr( $item->icon ); ?>" />
  190.                             <span><?php echo __('Input font awesome icon or icon class. You can see <a target="_blank" href="http://fortawesome.github.io/Font-Awesome/icons/">Font Awesome Icons in here</a>. For example: fa-users', 'porto') ?></span>
  191.                         </label>
  192.                     </p>
  193.                    
  194.                      <?php
  195.                     /* New fields insertion ends here */
  196.                     ?><div style="clear:both;"></div><br/>
  197.                     <p class="description description-wide">
  198.                         <label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
  199.                             <?php echo 'Title Attribute'; ?><br />
  200.                             <input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title"
  201.                                 <?php if (esc_attr( $item->post_excerpt )) : ?>
  202.                                    name="menu-item-attr-title[<?php echo $item_id; ?>]"
  203.                                 <?php endif; ?>
  204.                                    data-name="menu-item-attr-title[<?php echo $item_id; ?>]"
  205.                                    value="<?php echo esc_attr( $item->post_excerpt ); ?>" />
  206.                         </label>
  207.                     </p>
  208.                     <p class="description description-thin">
  209.                         <label for="edit-menu-item-classes-<?php echo $item_id; ?>">
  210.                             <?php echo 'CSS Classes (optional)'; ?><br />
  211.                             <input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes"
  212.                                 <?php if (esc_attr( implode(' ', $item->classes ) )) : ?>
  213.                                    name="menu-item-classes[<?php echo $item_id; ?>]"
  214.                                 <?php endif; ?>
  215.                                    data-name="menu-item-classes[<?php echo $item_id; ?>]"
  216.                                    value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" />
  217.                         </label>
  218.                     </p>
  219.                     <p class="description description-thin">
  220.                         <label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
  221.                             <?php echo 'Link Relationship (XFN)'; ?><br />
  222.                             <input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn"
  223.                                 <?php if (esc_attr( $item->xfn )) : ?>
  224.                                    name="menu-item-xfn[<?php echo $item_id; ?>]"
  225.                                 <?php endif; ?>
  226.                                    data-name="menu-item-xfn[<?php echo $item_id; ?>]"
  227.                                    value="<?php echo esc_attr( $item->xfn ); ?>" />
  228.                         </label>
  229.                     </p>
  230.                     <p class="description description-wide">
  231.                         <label for="edit-menu-item-description-<?php echo $item_id; ?>">
  232.                             <?php echo 'Description'; ?><br />
  233.                             <textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20"
  234.                                 <?php if (esc_html( $item->description )) : ?>
  235.                                       name="menu-item-description[<?php echo $item_id; ?>]"
  236.                                 <?php endif; ?>
  237.                                       data-name="menu-item-description[<?php echo $item_id; ?>]"
  238.                                     ><?php echo esc_html( $item->description ); // textarea_escaped ?></textarea>
  239.                             <span class="description"><?php echo 'The description will be displayed in the menu if the current theme supports it.'; ?></span>
  240.                         </label>
  241.                     </p>
  242.                     <div class="menu-item-actions description-wide submitbox">
  243.                         <?php if( 'custom' != $item->type && $original_title !== false ) : ?>
  244.                         <p class="link-to-original">
  245.                             <?php printf( 'Original: %s', '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
  246.                         </p>
  247.                         <?php endif; ?>
  248.                         <a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
  249.                             echo wp_nonce_url(
  250.                                 esc_url( add_query_arg(
  251.                                     array(
  252.                                         'action' => 'delete-menu-item',
  253.                                         'menu-item' => $item_id,
  254.                                     ),
  255.                                     esc_url( remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) ) )
  256.                                 ) ),
  257.                                 'delete-menu_item_' . $item_id
  258.                             ); ?>"><?php echo 'Remove'; ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), esc_url( remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) ) ) );
  259.                         ?>#menu-item-settings-<?php echo $item_id; ?>"><?php echo 'Cancel'; ?></a>
  260.                     </div>
  261.                     <input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
  262.                     <input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
  263.                     <input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
  264.                     <input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
  265.                     <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
  266.                     <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
  267.                     <div class="clear"></div>
  268.                 </div>
  269.                 <ul class="menu-item-transport"></ul>
  270.             </li>
  271.  
  272.         <?php
  273.             $output .= ob_get_clean();
  274.     }
  275.    
  276. }
  277. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement