函数文档

wp_set_lang_dir()

💡 云策文档标注

概述

wp_set_lang_dir() 函数用于设置 WordPress 语言目录的位置。它通过检查 WP_LANG_DIR 常量是否已定义,并根据目录存在情况自动定义该常量。

关键要点

  • 函数自动设置语言目录路径,优先使用 WP_CONTENT_DIR 下的 languages 目录。
  • 如果 WP_CONTENT_DIR/languages 不存在或 WPINC/languages 不存在,则回退到 WPINC/languages。
  • 定义 WP_LANG_DIR 常量时,路径为完整服务器路径,无前导或尾随斜杠。
  • 同时定义 LANGDIR 常量以保持向后兼容性,但可能在某些情况下不工作。

代码示例

function wp_set_lang_dir() {
    if ( ! defined( 'WP_LANG_DIR' ) ) {
        if ( file_exists( WP_CONTENT_DIR . '/languages' ) && @is_dir( WP_CONTENT_DIR . '/languages' )
            || ! @is_dir( ABSPATH . WPINC . '/languages' )
        ) {
            define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );
            if ( ! defined( 'LANGDIR' ) ) {
                define( 'LANGDIR', 'wp-content/languages' );
            }
        } else {
            define( 'WP_LANG_DIR', ABSPATH . WPINC . '/languages' );
            if ( ! defined( 'LANGDIR' ) ) {
                define( 'LANGDIR', WPINC . '/languages' );
            }
        }
    }
}

注意事项

要手动设置语言目录,应在 wp-config.php 中定义 WP_LANG_DIR 常量,以避免函数自动处理。


📄 原文内容

Sets the location of the language directory.

Description

To set directory manually, define the WP_LANG_DIR constant in wp-config.php.

If the language directory exists within WP_CONTENT_DIR, it is used. Otherwise the language directory is assumed to live in WPINC.

Source

function wp_set_lang_dir() {
	if ( ! defined( 'WP_LANG_DIR' ) ) {
		if ( file_exists( WP_CONTENT_DIR . '/languages' ) && @is_dir( WP_CONTENT_DIR . '/languages' )
			|| ! @is_dir( ABSPATH . WPINC . '/languages' )
		) {
			/**
			 * Server path of the language directory.
			 *
			 * No leading slash, no trailing slash, full path, not relative to ABSPATH
			 *
			 * @since 2.1.0
			 */
			define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );

			if ( ! defined( 'LANGDIR' ) ) {
				// Old static relative path maintained for limited backward compatibility - won't work in some cases.
				define( 'LANGDIR', 'wp-content/languages' );
			}
		} else {
			/**
			 * Server path of the language directory.
			 *
			 * No leading slash, no trailing slash, full path, not relative to `ABSPATH`.
			 *
			 * @since 2.1.0
			 */
			define( 'WP_LANG_DIR', ABSPATH . WPINC . '/languages' );

			if ( ! defined( 'LANGDIR' ) ) {
				// Old relative path maintained for backward compatibility.
				define( 'LANGDIR', WPINC . '/languages' );
			}
		}
	}
}

Changelog

Version Description
3.0.0 Introduced.