钩子文档

gettext_{$domain}

💡 云策文档标注

概述

gettext_{$domain} 是一个 WordPress 动态 Hook,用于过滤特定文本域的翻译字符串。它允许开发者修改插件或主题中的翻译文本,实现自定义显示内容。

关键要点

  • Hook 名称中的 $domain 是动态部分,指代文本域,确保过滤器仅应用于指定域。
  • 参数包括 $translation(已翻译文本)、$text(原始文本)和 $domain(文本域标识符)。
  • 通过 apply_filters 调用,常用于 translate() 函数中,从 WordPress 5.5.0 版本引入。
  • 用户贡献笔记提供了实际应用示例,如修改 WP Document Revisions 插件中的文本字符串。

代码示例

add_filter( 'gettext_wp-document-revisions', 'wpdocs_change_wp_document_revision_strings', 10, 3 );
/**
 * 代码片段:修改 WP Document Revisions 插件中的 "Owner" 文本
 *
 * @link https://developer.wordpress.org/reference/hooks/gettext_domain/
 */
function wpdocs_change_wp_document_revision_strings( $translated_text, $text, $domain ) {
    switch ( $text ) {
        case 'Owner':
            $translated_text = 'Submitter';
            break;
        case 'All owners':
            $translated_text = 'All submitters';
            break;
        case 'Document Owner':
            $translated_text = 'Submitted by';
            break;
        // 添加更多需要修改的字符串案例
    }
    return $translated_text;
}

注意事项

  • 由于 Hook 的动态特性,$domain 检查在代码中可能冗余,但建议保留以确保兼容性。
  • 示例代码应添加到主题的 functions.php 文件或自定义插件中,以安全地修改文本。

📄 原文内容

Filters text with its translation for a domain.

Description

The dynamic portion of the hook name, $domain, refers to the text domain.

Parameters

$translationstring
Translated text.
$textstring
Text to translate.
$domainstring
Text domain. Unique identifier for retrieving translated strings.

Source

$translation = apply_filters( "gettext_{$domain}", $translation, $text, $domain );

Changelog

Version Description
5.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Changing Texts in Plugins using the gettext_domain Filter

    Sometimes, when working with WordPress plugins, you might want to customize the displayed texts to better match your website’s content or branding. The `gettext_domain` filter provides a simple way to achieve this by allowing you to modify specific text strings used by a plugin.

    Here’s a practical example of how to use the `gettext_domain` filter to change text strings in the context of the WP Document Revisions plugin:

    Suppose you’re using the WP Document Revisions plugin and want to replace the term “Owner” with “Submitter” in various instances throughout your site.

    You can achieve this by adding the following code snippet to your theme’s `functions.php` file or a custom plugin:

    add_filter( 'gettext_wp-document-revisions', 'wpdocs_change_wp_document_revision_strings', 10, 3 );
    /**
     * Code snippet to change the "Owner" text in the WP Document Revisions plugin
     *
     * @link <a href="https://developer.wordpress.org/reference/hooks/gettext_domain/" rel="ugc">https://developer.wordpress.org/reference/hooks/gettext_domain/</a>
     */
    function wpdocs_change_wp_document_revision_strings( $translated_text, $text, $domain ) {
        if ( 'wp-document-revisions' === $domain ) {
            switch ( $text ) {
                case 'Owner':
                    $translated_text = 'Submitter';
                    break;
                case 'All owners':
                    $translated_text = 'All submitters';
                    break;
                case 'Document Owner':
                    $translated_text = 'Submitted by';
                    break;
    
                // Add more cases for other strings you want to change
    
            }
        }
    
        return $translated_text;
    }

  2. Skip to note 4 content

    Per @crstauf’s feedback, the code snippet should be simplified to:

    add_filter( 'gettext_wp-document-revisions', 'wpdocs_change_wp_document_revision_strings', 10, 3 );
    /**
     * Code snippet to change the "Owner" text in the WP Document Revisions plugin
     *
     * @link <a href="https://developer.wordpress.org/reference/hooks/gettext_domain/" rel="ugc">https://developer.wordpress.org/reference/hooks/gettext_domain/</a>
     */
    function wpdocs_change_wp_document_revision_strings( $translated_text, $text, $domain ) {
        switch ( $text ) {
            case 'Owner':
                $translated_text = 'Submitter';
                break;
            case 'All owners':
                $translated_text = 'All submitters';
                break;
            case 'Document Owner':
                $translated_text = 'Submitted by';
                break;
    
            // Add more cases for other strings you want to change.
    
        }
    
        return $translated_text;
    }