Created
October 1, 2018 05:37
-
-
Save akther80/70872b4e7a739a0c1f1e574faa83ec28 to your computer and use it in GitHub Desktop.
Add RTL Switcher in Menu Item
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
function electro_wp_set_direction() { | |
global $wp_locale, $wp_styles; | |
if ( isset( $_GET['d'] ) ) { | |
$direction = $_GET['d'] == 'rtl' ? 'rtl' : 'ltr'; | |
setcookie( 'electro_demo_direction', $direction, time()+3600 ); | |
} elseif( isset( $_COOKIE['electro_demo_direction'] ) ) { | |
$direction = $_COOKIE['electro_demo_direction']; | |
if ( false === $direction ) { | |
$direction = isset( $wp_locale->text_direction ) ? $wp_locale->text_direction : 'ltr'; | |
} | |
} | |
if( isset( $direction ) ) { | |
$wp_locale->text_direction = $direction; | |
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) { | |
$wp_styles = new WP_Styles(); | |
} | |
$wp_styles->text_direction = $direction; | |
} | |
} | |
add_action( 'init', 'electro_wp_set_direction' ); | |
function electro_rtl_switch_menu_nav_item( $items, $args ) { | |
$available_menu_locations = apply_filters( 'electro_rtl_switch_menu_item_locations', array( 'topbar-right' ) ); | |
if ( in_array( $args->theme_location, $available_menu_locations ) && apply_filters( 'electro_rtl_switch_menu_item', true ) ) { | |
if( is_rtl() ) { | |
$url = add_query_arg( array( 'd' => 'ltr' ) ); | |
$title = esc_html__( 'Switch to LTR', 'electro' ); | |
$item_title = apply_filters( 'electro_rtl_switch_menu_item_title', '<i class="fa fa-refresh"></i>' . $title ); | |
} else { | |
$url = add_query_arg( array( 'd' => 'rtl' ) ); | |
$title = esc_html__( 'Switch to RTL', 'electro' ); | |
$item_title = apply_filters( 'electro_rtl_switch_menu_item_title', '<i class="fa fa-refresh"></i>' . $title ); | |
} | |
$items .= '<li class="menu-item"><a title="' . esc_attr( $title ) . '" href="' . esc_url( $url ) . '">' . $item_title . '</a></li>'; | |
} | |
return $items; | |
} | |
add_filter( 'wp_nav_menu_items', 'electro_rtl_switch_menu_nav_item', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment