To translate a custom post type in WordPress using WPML, you need to follow a few steps. First, make sure you have the WPML plugin installed and activated on your WordPress site. Then, you’ll need to register your custom post type with WPML using the icl_register_string() function. This function allows WPML to recognize and translate the strings associated with your custom post type.
Here’s an example of how to register a custom post type for translation with WPML:
function wpsnippets_register_custom_post_type() {
$args = array(
// Your custom post type arguments here
);
register_post_type( 'your_custom_post_type', $args );
// Register custom post type strings for translation
$strings = array(
'singular_name' => 'Your Custom Post Type',
'plural_name' => 'Your Custom Post Types',
// Add more strings to translate if needed
);
foreach ( $strings as $string_key => $string_value ) {
$context = 'Custom Post Type';
$group = 'your_custom_post_type';
icl_register_string( $context, $string_key, $string_value, $group );
}
}
add_action( 'init', 'wpsnippets_register_custom_post_type' );
In the code snippet above, replace 'your_custom_post_type' with the slug of your custom post type, and update the $args array with your desired custom post type settings.
After registering the custom post type, you can use the icl_translate() function to retrieve the translated strings in your templates or plugin files. Here’s an example:
$translated_singular_name = icl_translate( 'Custom Post Type', 'singular_name', 'your_custom_post_type' );
echo $translated_singular_name;
In the code snippet above, 'singular_name' is the string key you used when registering the custom post type strings, and 'your_custom_post_type' is the group you assigned to the strings.
By following these steps, you can ensure that your custom post type is translatable with WPML, allowing you to create multilingual content on your WordPress site.
Examples
Example #1: Translating a Custom Post Type with WPML
This example demonstrates how to use WPML to translate a custom post type in WordPress. The code example shows how to register a custom post type and configure it for translation using WPML.
function wpsnippets_register_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wpsnippets_register_custom_post_type' );
function wpsnippets_translate_custom_post_type() {
if ( function_exists( 'icl_register_string' ) ) {
icl_register_string( 'Custom Post Types', 'Book', 'Book' );
}
}
add_action( 'wpml_register_single_string', 'wpsnippets_translate_custom_post_type' );
In this code example, we first register a custom post type called “Books” with the register_post_type() function. We then use the icl_register_string() function provided by WPML to register the custom post type for translation. This allows WPML to recognize and handle the translation of the custom post type.
Example #2: Translating Custom Fields of a Custom Post Type with WPML
This example demonstrates how to translate custom fields of a custom post type using WPML. The code example shows how to add custom fields to a custom post type and configure them for translation with WPML.
function wpsnippets_register_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wpsnippets_register_custom_post_type' );
function wpsnippets_register_custom_fields() {
if ( function_exists( 'register_field_group' ) ) {
register_field_group( array(
'title' => 'Book Details',
'fields' => array(
array(
'key' => 'field_book_author',
'label' => 'Author',
'name' => 'book_author',
'type' => 'text',
'instructions' => '',
'required' => false,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'book',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array(
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array(),
),
'menu_order' => 0,
) );
}
}
add_action( 'init', 'wpsnippets_register_custom_fields' );
function wpsnippets_translate_custom_fields() {
if ( function_exists( 'icl_register_string' ) ) {
icl_register_string( 'Custom Fields', 'Author', 'Author' );
}
}
add_action( 'wpml_register_single_string', 'wpsnippets_translate_custom_fields' );
In this code example, we first register a custom post type called “Books” with the register_post_type() function. We then use the register_field_group() function provided by the Advanced Custom Fields plugin to add a custom field for the author of the book. We also use the icl_register_string() function to register the custom field for translation with WPML.
Example #3: Translating Custom Taxonomies of a Custom Post Type with WPML
This example demonstrates how to translate custom taxonomies of a custom post type using WPML. The code example shows how to register a custom taxonomy and configure it for translation with WPML.
function wpsnippets_register_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wpsnippets_register_custom_post_type' );
function wpsnippets_register_custom_taxonomy() {
$args = array(
'labels' => array(
'name' => 'Genres',
'singular_name' => 'Genre',
),
'public' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', 'book', $args );
}
add_action( 'init', 'wpsnippets_register_custom_taxonomy' );
function wpsnippets_translate_custom_taxonomy() {
if ( function_exists( 'icl_register_string' ) ) {
icl_register_string( 'Custom Taxonomies', 'Genre', 'Genre' );
}
}
add_action( 'wpml_register_single_string', 'wpsnippets_translate_custom_taxonomy' );
In this code example, we first register a custom post type called “Books” with the register_post_type() function. We then register a custom taxonomy called “Genres” using the register_taxonomy() function. We also use the icl_register_string() function to register the custom taxonomy for translation with WPML.
