Content Translation with Shortcodes in WordPress

When developing a multilingual website on WordPress using plugins like Polylang, I often faced the challenge of translating specific parts of the site using shortcodes. This was especially crucial for media files, as duplicating the same file for each language was not ideal. I devised a convenient solution that can be applied not only to media files but also to various content elements on the site.

Simplifying Content Translation with Custom Shortcodes

To translate using shortcodes, you need to use this code in function.php:

add_shortcode( 'language', 'language_content' );
function language_content($atts,$content= null) {
	$locale = substr( get_bloginfo ( 'language' ), 0, 2 );
	$id = '';
	extract( shortcode_atts( array(
		'id' => $id,
    ), $atts ) );
	
	if ($id==$locale) {
		return $content;
	}
}

Detailed Code Explanation:

Shortcode Function Definition:

Using add_shortcode(), we define a shortcode named 'language' that calls the function language_content when used.

lang_content Function:

  • The language_content function processes the shortcode attributes and content.
  • It retrieves the current language locale and the specified ID from the shortcode attributes.
  • If the ID matches the current language, it returns the content as is.

Shortcode Usage Explanation:

[language id="en"]Your text pl[/language][language id="pl"]Your text en[/language]

Shortcode Implementation:

To use the shortcode, insert in the editor or any admin elements that support shortcodes.

Shortcode Breakdown:

  • [language id="en"]Your text[/language]: Displays ‘Your text’ only if the ID matches the English (‘en’) locale.
  • [language id="pl"]Your text[/language]: Displays ‘Your text’ only if the ID matches the Polish (‘pl’) locale.

By following this approach, you can easily manage content translation using shortcodes, ensuring a seamless multilingual experience for your WordPress website.

Using Translation in Template Files

In some cases, you may need to use the do_shortcode() function in your template files to utilize translation functions. As shown in the example below:

$post_thumbnail_id = get_post_thumbnail_id($post->ID);

$post_thumbnail_url = wp_get_attachment_image_url($post_thumbnail_id, 'full');

$post_thumbnail_title = get_post($post_thumbnail_id)->post_title;

echo '<img src="' . esc_url($post_thumbnail_url) . '" title="' . do_shortcode($post_thumbnail_title) . '" />';

This PHP code snippet is designed to display a reduced version of a WordPress post image with the ‘title’ attribute.

In the title attribute in the admin panel, we insert our shortcode [language id=”en”]Your text en[/language][language id=”pl”]Your text pl[/language], and thanks to the do_shortcode() function, we have translation.

Implementing custom shortcodes for content translation offers a practical and efficient way to handle multilingual content in WordPress. By leveraging this method, you can streamline the translation process and maintain consistency across different language versions of your website.