Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndreiTelteu/f4d5198f71018ac04293ab2f5dbcf45f to your computer and use it in GitHub Desktop.
Save AndreiTelteu/f4d5198f71018ac04293ab2f5dbcf45f to your computer and use it in GitHub Desktop.

In your theme's functions.php file, or in a custom plugin insert this code:

<?php

add_filter( 'rank_math/frontend/title', function( $title ) {
    $translatepress_translation = custom_get_translatepress_translation($title);
    if ($translatepress_translation != null) {
        $title = $translatepress_translation;
    }
	return $title;
});
add_filter( 'rank_math/frontend/description', function( $description ) {
    $translatepress_translation = custom_get_translatepress_translation($description);
    if ($translatepress_translation != null) {
        $description = $translatepress_translation;
    }
	return $description;
});

function custom_get_translatepress_translation($text, $prefix='', $suffix='') {
    // source: https://gist.github.com/AndreiTelteu/f4d5198f71018ac04293ab2f5dbcf45f
    global $TRP_LANGUAGE;
    global $custom_trp_query;
    if (!$custom_trp_query) {
        $trp = TRP_Translate_Press::get_trp_instance();
        $custom_trp_query = $trp->get_component( 'query' );
    }
    if ($prefix != '') $text = str_replace($prefix, '', $text);
    if ($suffix != '') $text = str_replace($suffix, '', $text);
    $translated = $custom_trp_query->get_existing_translations( array_values([
        $text,
    ]), $TRP_LANGUAGE );
    if ($translated && count($translated) > 0) {
        return $prefix.(array_values($translated)[0]->translated).$suffix;
    }
    return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment