• I am using this function to replace Divi with Framework

    add_filter('gettext', 'translate_text', 10);
    add_filter('ngettext', 'translate_text');
    function translate_text($translated) {
        $translated = str_ireplace('Divi',  'Framework',  $translated);
          return $translated;
    }

    It works fine as it replaced all instances of Divi with Framework. Now the problem is it also replaces Divider to Frameworkder

    How I can prevent this? I only want Divi to be replaced with my chosen text.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    You could use preg_replace to get specific, or something like this:

    function jp_filter_gettext( $translated, $original, $domain ) {
    
    $strings = array(
    	'Old' => 'New',
    );
    
    if ( isset( $strings[$original] ) ) {
    	$translations = get_translations_for_domain( $domain );
    	$translated = $translations->translate( $strings[$original] );
    }
    
    return $translated;
    }
    add_filter( 'gettext', 'jp_filter_gettext', 10, 3 );

    Thread Starter klmnweb

    (@klmnweb)

    Thanks mindctrl for the snippet. Unfortunately, it’s not outputting anything, not replacing the old text to new ones. Maybe I need make some adjustments. Any idea of improving this snippet?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I replace only specific text using gettext’ is closed to new replies.