How to Assign WPML Translators Based on User Roles

WordPress Multilingual (WPML) simplifies the process of creating multilingual websites. However, managing translators on a per-user basis can be time-consuming. In this tutorial, you’ll learn how to assign WPML translation capabilities to users with specific custom roles, such as custom_translator_role. By implementing a custom filter in your WordPress site, you can ensure that users belonging to the custom_translator_role and the existing editor role can act as translators without the need for individual user assignments.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Edit Your Child Theme’s functions.php

To begin, open your WordPress child theme’s functions.php file. This file serves as the central hub for customizing your theme’s features. You can edit it directly within the Theme Editor in your WordPress dashboard or use an external code editor in combination with FTP (File Transfer Protocol) for more advanced editing. Insert the provided code snippet at the file’s end to implement the desired functionality.

// Assign WPML Translators Based on User Roles.
add_filter(
    'wpml_override_is_translator',
    function ($is_translator, $user_id, $args) {
        $user = get_user_by('id', $user_id);

        // Define the user roles that can act as translators
        $allowed_roles = array('translator', 'custom_translator_role');

        // Check if the user belongs to any allowed role
        if (array_intersect($allowed_roles, (array) $user->roles)) {
            return true;
        }

        return $is_translator;
    },
    10,
    3
);
Code language: PHP (php)

Explanation:

  • add_filter('wpml_override_is_translator', ...): This line hooks into the wpml_override_is_translator filter, allowing you to modify WPML’s behavior for determining who can act as a translator.
  • function ($is_translator, $user_id, $args) { ... }: This anonymous function checks if the user specified by $user_id can act as a translator. The function receives the current translator status ($is_translator), user ID, and additional arguments as parameters.
  • $user = get_user_by('id', $user_id);: This line retrieves the user object based on the provided user ID.
  • $allowed_roles = array('editor', 'custom_translator_role');: Here, you define an array of allowed roles that can act as translators. In this example, ‘editor’ and ‘custom_translator_role’ are the allowed roles.
  • if (in_array($user->roles[0], $allowed_roles)) { return true; }: This conditional statement checks if the user’s primary role (the first role in the roles array) is in the $allowed_roles array. If the user belongs to any of these roles, they are granted translator privileges (return true).
  • return $is_translator;: If the user’s role is not in the allowed roles, the original translator status is preserved (return $is_translator), ensuring WPML’s default behavior for other users.

Note: If you’re using only the editor role for translation, you can omit custom_translator_role from the $allowed_roles array. The updated code will look like this:

$allowed_roles = array( 'editor' );
Code language: PHP (php)

Testing the Implementation

Log in to your WordPress dashboard with a user account that belongs to the ‘custom_translator_role‘ or ‘editor‘ role. Navigate to the WPML translation section. Users with these roles should now have access to translation tasks, enabling them to contribute as translators seamlessly.

That’s it! You’ve successfully configured WPML to assign translation capabilities based on specific user roles. This customization simplifies translation management, allowing users within designated roles to handle translations without the need for individual user assignments.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

2 Comments

    • Thank you for your comment, Jose! I’m thrilled to hear that the tutorial worked perfectly for you. Your feedback is priceless and motivates me to keep creating helpful content.

      Reply

Your thoughts matter, leave a reply 💬

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