Last updated on October 18, 2023

WPML RTL language support

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Enable Right-to-Left (RTL) languages in WPML.

To add WPML RTL (Right-to-Left) language support to your WordPress theme, you can use the wp_enqueue_style() function to enqueue the RTL CSS file provided by WPML. This will ensure that your theme’s styles are properly applied when a RTL language is selected.

Here’s an example code snippet that demonstrates how to enqueue the RTL CSS file in your theme’s functions.php file:

function wpsnippets_enqueue_wpml_rtl_styles() {
    if ( function_exists( 'wpml_is_rtl' ) && wpml_is_rtl() ) {
        wp_enqueue_style( 'wpml-rtl', WPML_PLUGIN_URL . '/res/css/rtl.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_enqueue_wpml_rtl_styles' );

In this code snippet, we first check if the wpml_is_rtl() function exists and if it returns true. This function is provided by the WPML plugin and checks if the current language is RTL. If it is, we enqueue the RTL CSS file using the wp_enqueue_style() function.

The wp_enqueue_scripts action hook is used to ensure that the RTL CSS file is enqueued on the front-end of your website.

By using this code snippet, your theme will automatically load the appropriate RTL styles when a RTL language is selected in WPML, providing a better user experience for your RTL language users.

Examples

Example 1: Enabling RTL Language Support in WPML

This example demonstrates how to enable RTL (Right-to-Left) language support in WPML.

function wpsnippets_enable_rtl_support() {
    if ( function_exists( 'is_rtl' ) && is_rtl() ) {
        add_filter( 'wpml_is_rtl', '__return_true' );
    }
}
add_action( 'wpml_loaded', 'wpsnippets_enable_rtl_support' );

The code checks if the current language is set to RTL using the is_rtl() function. If it is, the wpml_is_rtl filter is set to true using the add_filter() function. This enables RTL support in WPML.

Example 2: Customizing RTL Stylesheets in WPML

This example demonstrates how to customize RTL stylesheets in WPML by adding a custom CSS file.

function wpsnippets_custom_rtl_stylesheet() {
    if ( function_exists( 'is_rtl' ) && is_rtl() ) {
        wp_enqueue_style( 'custom-rtl-style', get_stylesheet_directory_uri() . '/rtl.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_custom_rtl_stylesheet' );

The code checks if the current language is set to RTL using the is_rtl() function. If it is, the wp_enqueue_style() function is used to enqueue a custom RTL stylesheet named rtl.css located in the theme’s directory.

Example 3: Modifying RTL Language Switcher in WPML

This example demonstrates how to modify the RTL language switcher in WPML by adding a custom class.

function wpsnippets_modify_rtl_language_switcher( $switcher_html ) {
    if ( function_exists( 'is_rtl' ) && is_rtl() ) {
        $switcher_html = str_replace( 'class="wpml-ls-item', 'class="wpml-ls-item custom-rtl-class', $switcher_html );
    }
    return $switcher_html;
}
add_filter( 'wpml_ls_languages', 'wpsnippets_modify_rtl_language_switcher' );

The code checks if the current language is set to RTL using the is_rtl() function. If it is, the str_replace() function is used to add a custom class (custom-rtl-class) to the language switcher HTML. The modified HTML is then returned using the wpml_ls_languages filter.

Last updated on October 18, 2023. Originally posted on January 19, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *