Advertisement
MoonWatch

WordPress Menu Walker, Custom Link with multilingual support

Nov 20th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. // You put this piece of code in your functions.php file
  2. class CustomLinkModifierWalker extends Walker_Nav_Menu {
  3.    function __( $text ) {
  4.       $text = urldecode( $text );
  5.       if ( preg_match_all('~(.*?)\|(\w{2,})\|~', $text, $matches) ) {
  6.          $text = '';
  7.          foreach ($matches[1] as $i => $match) {
  8.             $text .= "[:{$matches[2][$i]}]$match";
  9.          }
  10.          $text = __( $text );
  11.       }
  12.       return $text;
  13.    }
  14.    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  15.       global $wp_query;
  16.       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  17.  
  18.       $class_names = $value = '';
  19.  
  20.       $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  21.       $classes[] = 'menu-item-' . $item->ID;
  22.  
  23.       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  24.       $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  25.  
  26.       $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  27.       $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  28.  
  29.       $output .= $indent . '<li' . $id . $value . $class_names .'>';
  30.  
  31.       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  32.       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  33.       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  34.       $attributes .= ! empty( $item->url )    ? ' href="' . esc_attr( $this->__( $item->url )  ) .'"' : '';
  35.  
  36.       $item_output = $args->before;
  37.       $item_output .= '<a'. $attributes .'>';
  38.       $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  39.       $item_output .= '</a>';
  40.       $item_output .= $args->after;
  41.  
  42.       $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  43.    }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement