Last updated on October 18, 2023

WPML translate custom post statuses

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate custom post statuses with WPML.

To translate custom post statuses in WPML, you can use the icl_register_string() function provided by the WPML plugin. This function allows you to register custom strings for translation.

Here’s an example of how you can use icl_register_string() to translate a custom post status:

function wpsnippets_register_custom_post_status() {
    register_post_status( 'custom_status', array(
        'label'                     => _x( 'Custom Status', 'post' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>' ),
    ) );
}
add_action( 'init', 'wpsnippets_register_custom_post_status' );

function wpsnippets_translate_custom_post_status() {
    icl_register_string( 'Post Status', 'Custom Status', 'Custom Status' );
}
add_action( 'init', 'wpsnippets_translate_custom_post_status' );

In this example, we first register a custom post status called ‘custom_status’ using the register_post_status() function. We provide a label for the post status using the _x() function, which allows for translation.

Next, we use the icl_register_string() function to register the string ‘Custom Status’ for translation. The first parameter is the context, which is set to ‘Post Status’ in this case. The second parameter is the string to be translated, and the third parameter is the default value for the string.

By registering the custom post status label as a translatable string, WPML will be able to detect and provide translation options for it in the WPML translation interface.

This code snippet can be useful when you have custom post statuses in your WordPress site and you want to translate them using WPML. By registering the custom post status label as a translatable string, you can easily provide translations for different languages using WPML’s translation management features.

Examples

Example 1: Translating custom post statuses using WPML

This example demonstrates how to translate custom post statuses using the WPML plugin in WordPress. The code snippet below shows how to register a custom post status and translate it using WPML.

function wpsnippets_register_custom_post_status() {
    register_post_status( 'my_custom_status', array(
        'label'                     => _x( 'My Custom Status', 'post' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'My Custom Status <span class="count">(%s)</span>', 'My Custom Status <span class="count">(%s)</span>' ),
    ) );
}
add_action( 'init', 'wpsnippets_register_custom_post_status' );

In this code example, we use the register_post_status() function to register a custom post status called “My Custom Status”. The _x() function is used to provide a translatable label for the status. WPML will automatically detect this translatable string and make it available for translation in the WPML translation interface.

Example 2: Translating custom post statuses in WPML translation interface

This example demonstrates how to translate custom post statuses using the WPML translation interface. After registering a custom post status, you can easily translate it using the WPML plugin.

  1. Install and activate the WPML plugin.
  2. Go to the WPML -> String Translation page in the WordPress admin.
  3. Search for the translatable string “My Custom Status” and select it.
  4. Enter the translated string for the selected language and save the translation.

WPML will now use the translated string for the custom post status in the selected language.

Example 3: Displaying translated custom post statuses in frontend

This example demonstrates how to display translated custom post statuses in the frontend of your WordPress site. You can use the wpsnippets_get_custom_post_status() function to retrieve the translated label for a custom post status.

function wpsnippets_get_custom_post_status( $post_status ) {
    $translated_status = '';

    if ( function_exists( 'icl_t' ) ) {
        $translated_status = icl_t( 'post status', $post_status, $post_status );
    }

    return $translated_status;
}

In this code example, we define a custom function wpsnippets_get_custom_post_status() that takes a post status as a parameter. Inside the function, we use the icl_t() function provided by WPML to retrieve the translated label for the given post status. The translated label is then returned by the function.

Last updated on October 18, 2023. Originally posted on November 7, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *