函数文档

is_rtl()

💡 云策文档标注

概述

is_rtl() 函数用于检测当前语言环境是否为从右到左(RTL)布局,常用于主题和插件开发中根据语言方向加载相应样式或脚本。自 WordPress 3.0 起,推荐使用此函数替代 get_bloginfo('text_direction')。

关键要点

  • is_rtl() 返回布尔值,表示当前语言环境是否为 RTL。
  • 该函数内部依赖 WP_Locale 类,通过 $wp_locale->is_rtl() 实现检测。
  • 在 WordPress 3.0 中引入,取代了 get_bloginfo('text_direction') 的使用。
  • 广泛应用于样式加载、编辑器设置、语言属性获取等多个核心功能中。

代码示例

if ( is_rtl() ) {
  wp_enqueue_style(  'style-rtl',  plugins_url( '/css/style-rtl.css', __FILE__ ) );
  wp_enqueue_script( 'script-rtl', plugins_url( '/js/script-rtl.js',  __FILE__ ) );
}

📄 原文内容

Determines whether the current locale is right-to-left (RTL).

Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Return

bool Whether locale is RTL.

More Information

Deprecate get_bloginfo(‘text_direction’) in favor of is_rtl() in Version 3.0.

Source

function is_rtl() {
	global $wp_locale;
	if ( ! ( $wp_locale instanceof WP_Locale ) ) {
		return false;
	}
	return $wp_locale->is_rtl();
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes