Last updated on October 18, 2023

WPML translate plugin documentation

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

Translate plugin documentation with WPML.

To create WPML translate plugin documentation, you can use the WordPress built-in function register_post_type() to register a custom post type for your documentation. This will allow you to create and manage documentation pages in the WordPress admin area.

Here’s an example code snippet that demonstrates how to register a custom post type for your WPML translate plugin documentation:

function wpsnippets_register_documentation_post_type() {
    $labels = array(
        'name'                  => _x( 'Documentation', 'Post type general name', 'textdomain' ),
        'singular_name'         => _x( 'Documentation', 'Post type singular name', 'textdomain' ),
        'menu_name'             => _x( 'Documentation', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'        => _x( 'Documentation', 'Add New on Toolbar', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New Documentation', 'textdomain' ),
        'new_item'              => __( 'New Documentation', 'textdomain' ),
        'edit_item'             => __( 'Edit Documentation', 'textdomain' ),
        'view_item'             => __( 'View Documentation', 'textdomain' ),
        'all_items'             => __( 'All Documentation', 'textdomain' ),
        'search_items'          => __( 'Search Documentation', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent Documentation:', 'textdomain' ),
        'not_found'             => __( 'No documentation found.', 'textdomain' ),
        'not_found_in_trash'    => __( 'No documentation found in Trash.', 'textdomain' ),
        'featured_image'        => _x( 'Documentation Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'              => _x( 'Documentation archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'      => _x( 'Insert into documentation', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this documentation', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'     => _x( 'Filter documentation list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation' => _x( 'Documentation list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'            => _x( 'Documentation list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'documentation' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );

    register_post_type( 'documentation', $args );
}
add_action( 'init', 'wpsnippets_register_documentation_post_type' );

This code snippet registers a custom post type called “Documentation” with various labels and settings. It supports common features like title, editor, author, thumbnail, excerpt, and comments. The documentation pages will have a URL structure like example.com/documentation/sample-documentation.

You can place this code in your theme’s functions.php file or in a custom plugin file. After adding the code, you will see a new menu item called “Documentation” in the WordPress admin area, where you can create and manage your WPML translate plugin documentation pages.

Examples

Example 1: Displaying Translated Content Using WPML

This use case demonstrates how to display translated content on a WordPress website using the WPML plugin. The code example shows how to use the icl_translate function to retrieve the translated version of a specific string.

<?php
$translated_text = icl_translate('wpml-snippets', 'Hello, world!', 'fr');
echo $translated_text;
?>

In this code example, the icl_translate function is used to retrieve the translated version of the string 'Hello, world!' in French. The first parameter 'wpml-snippets' is the context, which helps WPML identify the correct translation. The second parameter is the string to be translated, and the third parameter 'fr' specifies the target language.

Example 2: Translating Dynamic Content Using WPML

This use case demonstrates how to translate dynamic content on a WordPress website using the WPML plugin. The code example shows how to use the icl_t function to translate a dynamic string that includes placeholders.

<?php
$name = 'John';
$translated_text = icl_t('wpml-snippets', 'Hello, %s!', array($name));
echo $translated_text;
?>

In this code example, the icl_t function is used to translate the string 'Hello, %s!', where %s is a placeholder for a dynamic value. The first parameter 'wpml-snippets' is the context, the second parameter is the string to be translated, and the third parameter array($name) provides the dynamic value to be inserted into the translated string.

Example 3: Translating Custom Fields Using WPML

This use case demonstrates how to translate custom fields on a WordPress website using the WPML plugin. The code example shows how to use the icl_register_string and icl_translate_string functions to translate a custom field value.

<?php
$field_name = 'my_custom_field';
$field_value = get_post_meta(get_the_ID(), $field_name, true);

icl_register_string('wpml-snippets', $field_name, $field_value);
$translated_value = icl_translate_string('wpml-snippets', $field_name, $field_value);
echo $translated_value;
?>

In this code example, the get_post_meta function is used to retrieve the value of a custom field named 'my_custom_field'. The icl_register_string function is then used to register the custom field value for translation. Finally, the icl_translate_string function is used to retrieve the translated version of the custom field value.

Last updated on October 18, 2023. Originally posted on December 21, 2023.

Leave a Reply

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