Advertisement
Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. <nav id="main-menu" class="main-menu">
  2. <div class="main-menu-top">
  3. <div class="main-menu-search"><?php get_search_form(); ?></div>
  4. <div class="main-menu-logo"><a href="/"><img src="logo.png" alt"website title" title="website title"></div></a>
  5. </div>
  6. <div class="main-menu-panel">
  7. <ul>
  8. <li class="main-menu-item">
  9. <a href="/"><span class="fas fa-home"></span>Home</a>
  10. </li>
  11. <li class="main-menu-item">
  12. <a href="/"><span class="fas fa-calendar-plus"></span>Updates</a>
  13. </li>
  14. <li class="main-menu-item main-menu-item-before-divider">
  15. <a href="/"><span class="fas fa-comments"></span>Live</a>
  16. </li>
  17. <li class="main-menu-item main-menu-item-divider">
  18. Berichten
  19. </li>
  20. <li class="main-menu-item">
  21. <a href="/"><span class="fas fa-tv"></span>Televisie</a>
  22. </li>
  23. <li class="main-menu-item main-menu-item-selected">
  24. <a href="/"><span class="fas fa-film"></span>Film</a>
  25. </li>
  26. <li class="main-menu-item main-menu-item">
  27. <a href="/"><span class="fas fa-headphones"></span>Radio</a>
  28. </li>
  29. <li class="main-menu-item main-menu-item-before-divider">
  30. <a href="/"><span class="fas fa-plus"></span>Andere</a>
  31. </li>
  32. <li class="main-menu-item main-menu-item-divider">
  33. Database
  34. </li>
  35. <li class="main-menu-item">
  36. <a href="/"><span class="fas fa-tv"></span>Televisie</a>
  37. </li>
  38. <li class="main-menu-item main-menu-item">
  39. <a href="/"><span class="fas fa-film"></span>Film</a>
  40. </li>
  41. <li class="main-menu-item">
  42. <a href="/"><span class="fas fa-headphones"></span>Radio</a>
  43. </li>
  44. </ul>
  45. </div>
  46. <div class="main-menu-bottom">
  47. <div class="main-menu-bottom-social social-mail">
  48. <a href="mailto:myemail"><span class="fas fa-envelope"></span></a>
  49. </div>
  50. <div class="main-menu-bottom-social social-facebook">
  51. <a href="https://www.facebook.com/myfacebook" target="_blank"><span class="fab fa-facebook-f"></span></a>
  52. </div>
  53. <div class="main-menu-bottom-social social-twitter">
  54. <a href="https://www.twitter.com/mytwitter" target="_blank"><span class="fab fa-twitter"></span></a>
  55. </div>
  56. </div>
  57. </nav>
  58.  
  59. <?php
  60. /**
  61. * Proof of concept for how to add new fields to nav_menu_item posts in the WordPress menu editor.
  62. * @author Weston Ruter (@westonruter), X-Team
  63. */
  64.  
  65. add_action( 'init', array( 'XTeam_Nav_Menu_Item_Custom_Fields', 'setup' ) );
  66.  
  67. class XTeam_Nav_Menu_Item_Custom_Fields {
  68. static $options = array(
  69. 'item_tpl' => '
  70. <p class="additional-menu-field-{name} description description-thin">
  71. <label for="edit-menu-item-{name}-{id}">
  72. {label}<br>
  73. <input
  74. type="{input_type}"
  75. id="edit-menu-item-{name}-{id}"
  76. class="widefat code edit-menu-item-{name}"
  77. name="menu-item-{name}[{id}]"
  78. value="{value}">
  79. </label>
  80. </p>
  81. ',
  82. );
  83.  
  84. static function setup() {
  85. if ( !is_admin() )
  86. return;
  87.  
  88. $new_fields = apply_filters( 'xteam_nav_menu_item_additional_fields', array() );
  89. if ( empty($new_fields) )
  90. return;
  91. self::$options['fields'] = self::get_fields_schema( $new_fields );
  92.  
  93. add_filter( 'wp_edit_nav_menu_walker', function () {
  94. return 'XTeam_Walker_Nav_Menu_Edit';
  95. });
  96. //add_filter( 'xteam_nav_menu_item_additional_fields', array( __CLASS__, '_add_fields' ), 10, 5 );
  97. add_action( 'save_post', array( __CLASS__, '_save_post' ), 10, 2 );
  98. }
  99.  
  100. static function get_fields_schema( $new_fields ) {
  101. $schema = array();
  102. foreach( $new_fields as $name => $field) {
  103. if (empty($field['name'])) {
  104. $field['name'] = $name;
  105. }
  106. $schema[] = $field;
  107. }
  108. return $schema;
  109. }
  110.  
  111. static function get_menu_item_postmeta_key($name) {
  112. return '_menu_item_' . $name;
  113. }
  114.  
  115. /**
  116. * Inject the
  117. * @hook {action} save_post
  118. */
  119. static function get_field( $item, $depth, $args ) {
  120. $new_fields = '';
  121. foreach( self::$options['fields'] as $field ) {
  122. $field['value'] = get_post_meta($item->ID, self::get_menu_item_postmeta_key($field['name']), true);
  123. $field['id'] = $item->ID;
  124. $new_fields .= str_replace(
  125. array_map(function($key){ return '{' . $key . '}'; }, array_keys($field)),
  126. array_values(array_map('esc_attr', $field)),
  127. self::$options['item_tpl']
  128. );
  129. }
  130. return $new_fields;
  131. }
  132.  
  133. /**
  134. * Save the newly submitted fields
  135. * @hook {action} save_post
  136. */
  137. static function _save_post($post_id, $post) {
  138. if ( $post->post_type !== 'nav_menu_item' ) {
  139. return $post_id; // prevent weird things from happening
  140. }
  141.  
  142. foreach( self::$options['fields'] as $field_schema ) {
  143. $form_field_name = 'menu-item-' . $field_schema['name'];
  144. // @todo FALSE should always be used as the default $value, otherwise we wouldn't be able to clear checkboxes
  145. if (isset($_POST[$form_field_name][$post_id])) {
  146. $key = self::get_menu_item_postmeta_key($field_schema['name']);
  147. $value = stripslashes($_POST[$form_field_name][$post_id]);
  148. update_post_meta($post_id, $key, $value);
  149. }
  150. }
  151. }
  152.  
  153. }
  154.  
  155. // @todo This class needs to be in it's own file so we can include id J.I.T.
  156. // requiring the nav-menu.php file on every page load is not so wise
  157. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  158. class XTeam_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
  159. function start_el(&$output, $item, $depth = 0, $args = [], $id = 0) {
  160. $item_output = '';
  161. parent::start_el($item_output, $item, $depth, $args, $id);
  162. // Inject $new_fields before: <div class="menu-item-actions description-wide submitbox">
  163. if ( $new_fields = XTeam_Nav_Menu_Item_Custom_Fields::get_field( $item, $depth, $args ) ) {
  164. $item_output = preg_replace('/(?=<div[^>]+class="[^"]*submitbox)/', $new_fields, $item_output);
  165. }
  166. $output .= $item_output;
  167. }
  168. }
  169.  
  170.  
  171. // Somewhere in config...
  172. add_filter( 'xteam_nav_menu_item_additional_fields', 'mytheme_menu_item_additional_fields' );
  173. function mytheme_menu_item_additional_fields( $fields ) {
  174. $fields['fa-class'] = array(
  175. 'name' => 'fa-class',
  176. 'label' => __('Font Awesome Class', 'xteam'),
  177. 'container_class' => 'fa-class',
  178. 'input_type' => 'text',
  179. );
  180.  
  181. return $fields;
  182. }
  183.  
  184. class Custom_Walker_Nav_Primary extends Walker_Nav_menu {
  185.  
  186. function start_lvl( &$output, $depth = 0, $args = array() ){ //ul
  187. $indent = str_repeat("t",$depth);
  188. $submenu = ($depth > 0) ? ' sub-menu' : '';
  189. $output .= "n$indent<ul class="dropdown-menu$submenu depth_$depth">n";
  190. }
  191.  
  192. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
  193.  
  194. $indent = ( $depth ) ? str_repeat("t",$depth) : '';
  195.  
  196. $li_attributes = '';
  197. $class_names = $value = '';
  198.  
  199. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  200. $classes[] = 'main-menu-item'; // common class for li items
  201. $classes[] = ($args->walker->has_children) ? 'dropdown' : '';
  202. $classes[] = ($item->current || $item->current_item_ancestor) ? 'active main-menu-item-selected ' : ''; // Add selected class for current menu item
  203. $classes[] = 'menu-item-' . $item->ID;
  204. if( $depth && $args->walker->has_children ){
  205. $classes[] = 'dropdown-submenu';
  206. }
  207.  
  208. $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  209. $class_names = ' class="' . esc_attr($class_names) . '"';
  210.  
  211. $id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
  212. $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  213.  
  214. $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
  215.  
  216. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
  217. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr($item->target) . '"' : '';
  218. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
  219. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr($item->url) . '"' : '';
  220.  
  221. $attributes .= ( $args->walker->has_children ) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
  222.  
  223.  
  224. $item_output = $args->before;
  225. $item_output .= '<a' . $attributes . '>';
  226.  
  227. $fa_class = get_post_meta($item->ID,'_menu_item_fa-class',true) ?: false; // Get font awesome class
  228. if( $fa_class ){
  229.  
  230. $item_output .= '<span class="'.$fa_class.'"></span>'; // Add span if the fa classes exists
  231.  
  232. }
  233.  
  234. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  235. $item_output .= ( $depth == 0 && $args->walker->has_children ) ? ' <b class="caret"></b></a>' : '</a>';
  236. $item_output .= $args->after;
  237.  
  238. $output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  239.  
  240. }
  241.  
  242. }
  243.  
  244. wp_nav_menu( array('menu' => 'Main', 'container' => '', 'items_wrap' => '<ul class="nav navbar-nav" id="mymenu">%3$s</ul>' , 'walker' => new Custom_Walker_Nav_Primary())); // you may need to change some args as per your requirement for example "menu" => "Some menu"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement