Skip to content

Instantly share code, notes, and snippets.

@svebal
Created July 26, 2017 09:08
Show Gist options
  • Save svebal/f6b3b249335d01c16c1724ddb8cfe213 to your computer and use it in GitHub Desktop.
Save svebal/f6b3b249335d01c16c1724ddb8cfe213 to your computer and use it in GitHub Desktop.
Creating an own markup for MultilingualPress
<?php
/**
* Create a navigation between translated instances
*
* @param array $args {
* Optional. Array of nav menu arguments.
*
* @param string $container Whether to wrap the language switch, and what to wrap it with. Default 'div'.
* @param string $container_class Class that is applied to the container. Default 'language-switch'.
*
* @param string $before Text before the link markup. Default empty.
* @param string $after Text after the link markup. Default empty.
* @param string $between Separator between items. Default empty
*
* @param string $items_wrap How the list items should be wrapped. Default is 'ul'.
* @param string $link_wrap Text before the link text. Default is 'li'.
*
* @param bool $show_current_locale Option for shwowing the curent locale shortcut. Default true.
* }
*
* @return string
*/
function mlp_navigation( $args = array() ) {
$defaults = array(
'container' =>'div',
'container_class' =>'language-switch',
'before' => '',
'after' => '',
'between' => '',
'items_wrap' => 'nav',
'items_wrap_class' => 'language-dropdown-list',
'link_wrap' => '',
'show_current_locale' => TRUE,
);
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'mlp_menu_args', $args );
$args = (object) $args;
$links = (array) mlp_get_interlinked_permalinks();
if ( empty ( $links ) ) {
return '';
}
$items = array ();
$output = '<' . $args->container . ' class="' . $args->container_class . '">';
if( $args->container !== FALSE ) {
$lang_code = get_locale();
$lang_first = strtok( $lang_code, '_' );
$lang_text = mb_strtoupper( $lang_first );
$output .= '<div class="language-dropdown-toggle">';
$output .= '<div class="switch-label">' . $lang_text . '</div>';
$output .= '<div class="icon-2 w-icon-dropdown-toggle"></div>';
$output .= '</div>';
}
$output .= '<' . $args->items_wrap . ' class="' . $args->items_wrap_class . '">';
foreach ( $links as $link ) {
if ( isset ( $link['text'] ) ) {
$text = $link['text'];
}
else {
// take just the main code
$first = strtok( $link['lang'], '_' );
$text = mb_strtoupper( $first );
}
if( ! empty( $args->link_wrap ) ) {
$items[] = sprintf(
'<' . $args->link_wrap . '>' .
'<a href="%1$s" hreflang="%2$s" rel="alternate">%3$s</a>' .
'</' . $args->link_wrap . '>',
esc_url( $link['permalink'] ),
esc_attr( $link['lang'] ),
$text
);
} else {
$items[] = sprintf(
'<a href="%1$s" hreflang="%2$s" rel="alternate">%3$s</a>' .
'</' . $args->link_wrap . '>',
esc_url( $link['permalink'] ),
esc_attr( $link['lang'] ),
$text
);
}
}
$output .= $args->before . join( $args->between, $items ) . $args->after;
if( ! empty( $args->link_wrap) ) {
$output .= '</' . $args->items_wrap . '>';
}
$output .= '</' . $args->container . '>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment