Skip Navigation
availability:

WPML Version: 3.2.3

description:
  • Gets the post type or taxonomy slug translation in a specific language.
  • The translation value is saved in WPMLSettingsPost Types Translation.
type:
filter
category:
Retrieving Localized Content
parameters:
$translated_slug = apply_filters( 'wpml_get_translated_slug', string $slug, string $post_type, string $language_code, string $element_type );
$slug
(string) (Required) The value is set in the rewrite argument of the register_post_type function.
$post_type
(string) (Required) The first parameter value of the register_post_type or register_taxonomy function.
$language_code
(string) (Optional) Return the translation in this language. The default is NULL which returns the current language.
$element_type
(string) (Optional) Specifies if the translated slug applies to a custom post type or custom taxonomy. Accepted values are post or taxonomy. The default is post.
hook example usage:

Post Type Example

Register a book post type with the custom slug my-book.

In WPMLSettingsPost Types Translation, translate the custom slug into Spanish with the value mi-libro.

Use the wpml_get_translated_slug hook to get the Spanish translation of this custom slug, which is mi-libro.

The example for wpml_get_translated_slug.
The example for wpml_get_translated_slug.
// Register a "book" post type with the custom slug "my-book"
add_action( 'init', 'register_book_init' );
 
function register_book_init() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' )
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'rewrite'            => array( 'slug' => 'my-book' ),
    );
 
    register_post_type( 'book', $args ); 
}
 
// Get the Spanish translation of the post type slug "my-book" 
// This value is saved in WPML->Settings->Post Types Translation
$translated_slug = apply_filters( 'wpml_get_translated_slug', 'my-book', 'book' ,'es' , 'post');
// Return "mi-libro"
echo $translated_slug;

// Register a "genre" custom taxonomy to the "book" custom post type
add_action( 'init', 'register_genre_init', 0 );

function register_genre_init() {
	register_taxonomy( 'genre', 'book', array(
		'rewrite'      => array( 'slug' => 'genre' )
	) );
}

Taxonomy Example

Register a genre taxonomy type with the custom slug genre.

In WPMLTaxonomy translation, translate the taxonomy term into Spanish with the value género.

Use the wpml_get_translated_slug hook to get the Spanish translation of this custom slug, which is género.

// Get the Spanish translation of the custom taxonomy slug "genre" 
// This value is saved in WPML->Settings->Taxonomies Translation
$translated_taxonomy_slug = apply_filters( 'wpml_get_translated_slug', 'genre', 'genre' ,'es', 'taxonomy' );
// Return "género"
echo $translated_taxonomy_slug;