Last active
September 5, 2019 16:03
-
-
Save Phoenix2k/24539d384076be4d9ad5 to your computer and use it in GitHub Desktop.
WPML: Create transient menus with JS support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove active states from navigation items (use JS instead) | |
* | |
* @link http://codex.wordpress.org/Function_Reference/wp_nav_menu | |
*/ | |
add_filter( 'wp_nav_menu', 'remove_active_nav_classes' ); | |
function remove_active_nav_classes( $classes ) { | |
$replace = array( | |
'current-menu-item' => '', | |
'current-menu-parent' => '', | |
'current-menu-ancestor' => '', | |
'current-page-item' => '', | |
'current-page-parent' => '', | |
'current-page-ancestor' => '', | |
'current_page_item' => '', | |
'current_page_parent' => '', | |
'current_page_ancestor' => '', | |
); | |
$classes = str_replace( array_keys( $replace ), $replace, $classes ); | |
return $classes; | |
} | |
/** | |
* Use WPML's icl_get_languages() to delete different language versions of transients | |
* | |
* @link http://codex.wordpress.org/Transients_API | |
*/ | |
add_action( 'wp_update_nav_menu', 'theme_update_nav_menu' ); | |
function theme_update_nav_menu() { | |
if ( function_exists( 'icl_get_languages' ) ) { | |
$languages = icl_get_languages( 'skip_missing=0&orderby=code' ); | |
if ( ! empty( $languages ) ) { | |
foreach ( $languages as $language ) { | |
delete_transient( 'header-menu-' . $language['language_code'] ); | |
delete_transient( 'footer-menu-' . $language['language_code'] ); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav id="header-navigation" role="navigation"> | |
<?php the_header_menu(); ?> | |
</nav> | |
<script> | |
// Set active classes to Header navigation based on current location | |
(function( $ ) { | |
var current_url = '/' + window.location.pathname.split('/')[1] + '/', | |
custom_url = location.href.split('#')[0]; | |
$('#header-navigation').find( 'a[href="' + current_url + '"], a[href="' + custom_url + '"]' ).parent().addClass('current-menu-item').parents('.menu-item').addClass('current-menu-ancestor active'); | |
})( jQuery ); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Register navigation menus | |
* | |
* @link http://codex.wordpress.org/Function_Reference/register_nav_menus | |
*/ | |
register_nav_menus(array( | |
'header' => __( 'Header navigation', 'theme-name' ), | |
'footer' => __( 'Footer navigaiton', 'theme-name' ), | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment