【问题标题】:Replace multiple strings or texts替换多个字符串或文本
【发布时间】:2021-05-19 02:27:18
【问题描述】:

如果您必须在任何页面中替换或翻译 WordPress 中的一个字符串或文本,functions.php 中的这段代码可以很好地工作:

//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');

//function
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text to replace', 'new text', $cancellation_btn);
return $cancellation_btn;
}

如何使用相同的代码替换多个字符串或文本?

在我的例子中,我用两个不同的按钮替换了文本,为此我使用了两次相同的代码:

//The filters.. both are required.
add_filter('gettext', 'change_cancellation_btn');
add_filter('ngettext', 'change_cancellation_btn');

//function button 1
function change_cancellation_btn($cancellation_btn){
$cancellation_btn = str_ireplace('text 1 to replace', 'new text 1', $cancellation_btn);
return $cancellation_btn;
}


//The filters.. both are required.
add_filter('gettext', 'change_nocancellation_btn');
add_filter('ngettext', 'change_nocancellation_btn');

//function button 2
function change_nocancellation_btn($nocancellation_btn){
$nocancellation_btn = str_ireplace('text 2 to replace', 'new text 2', $nocancellation_btn);
return $nocancellation_btn;
}

那么,有没有办法用一个函数替换多个字符串?

【问题讨论】:

  • str_ireplace() 可以接受数组,见手册。
  • 谢谢,我明白了,但是我对php不是很熟悉

标签: php wordpress string function replace


【解决方案1】:

您可以使用如下开关盒:

add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
  
    switch ( $translated_text ) {
 
        case 'text 1 to replace' :
 
            $translated_text = __( 'new text 1', 'theme_text_domain' );
            break;
 
        case 'text 2 to replace' :
 
            $translated_text = __( 'new text 2', 'theme_text_domain' );
            break;
    }    
 
    return $translated_text;
}

未测试。

【讨论】:

    【解决方案2】:
    Call the function inside the loop
    and pass the parameter by creating an array or if you from a database similar its work.
    
    e.g:
    $array = array(
    array(
    'text_to_replace' => 'Dummy',
    'new_text' => 'Test',
    'text_content'=> 'Dummy content string'
    ))
    
    //function
    function change_cancellation_btn($text_to_replace, $new_text, $text_content){
    $changedText = str_ireplace($text_to_replace, $new_text, $text_content);
    return $changedText;
    }
    
    
    Now call the change_cancellation_btn by running the loop of the array by passing multiple contents.
    

    【讨论】:

      【解决方案3】:

      更新。这对我来说效果更好,因为它也替换/翻译了管理后端中的所有字符串:

      add_filter(  'gettext',  'dirty_translate'  );
      add_filter(  'ngettext',  'dirty_translate'  );
      function dirty_translate( $translated ) {
           $words = array(
                  // 'word to translate' => 'translation'
                  'Dashboard' => 'Foo',
                  'Add new' => 'Bar'
           );
      $translated = str_ireplace(  array_keys($words),  $words,  $translated );
      return $translated;
      }
      

      参考:Wordpress replace all ocurrances of the string in admin

      【讨论】:

        猜你喜欢
        • 2018-04-04
        • 2017-03-20
        • 1970-01-01
        • 2017-08-01
        • 2015-03-15
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 2013-02-25
        相关资源
        最近更新 更多