RTL/LTR frontend switcher

I have a theme that supports RTL. When Appearance->Theme settings->Right-to-Left (RTL) text orientation is enabled theme switches correctly. But I want to add to the main menu 2 items to switch orientation from frontend:

  1. RTL - switch theme to RTL mode
  2. LTR - switch theme to LTR mode

How can I do it?

Topic switch front-end Wordpress

Category Web


Ok. I resolved it in this way. Let's say that we have a CSS-file rtl.css, which contains rules for right-to-left content direction.

Add menu items: Appearance->Menus->Select a menu to edit->Edit menus->Custom links.

  1. URL - ?language=rlt (or something like this), Link text - RTL (for example)
  2. URL - ?language=ltr, Link text - LTR

Create a plugin:

add_action('init', 'get_check');

function get_check(){
    if(!session_id()) {
        session_start();
    }

    if($_GET['language'] == 'rtl'){
        $_SESSION['rtl']=true;
    }

    if($_SESSION['rtl']){
        wp_enqueue_style( 'tmpl_rtl_css', get_template_directory_uri().'/rtl.css',array('tmpl_dir_css') );      
    }

    if($_GET['language'] == 'ltr'){
        session_destroy ();
        wp_dequeue_style('tmpl_rtl_css');
    }
}

There need checking, but solution is something like this.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.