Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Created September 27, 2017 10:48
Show Gist options
  • Save Mosharush/5e69d0c0cf61333e7cfd464b471986c1 to your computer and use it in GitHub Desktop.
Save Mosharush/5e69d0c0cf61333e7cfd464b471986c1 to your computer and use it in GitHub Desktop.
WordPress PolyLang translate shortcode & function - use in customizer texts
<?php
// If Polylang is active, hook function on the admin pages
if ( function_exists( 'pll_register_string' ) ) add_action( 'admin_init', 'pll_tc_strings_setup' );
function pll_tc_strings_setup() {
// Add text to Polylang's string translation panel
pll_register_string( 'fl-topbar-col1-text', 'Contact_Data', 'wipi', true );
pll_register_string( 'fl-arrow-breadcrumbs', 'arrow_mulilang', 'wipi', true );
}
function polylanguage_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'text' => '',
'lang' => pll_current_language()
),
$atts
);
return pll_translate_string( $atts['text'], $atts['lang'] );
}
add_shortcode( 'pll_translate', 'polylanguage_shortcode' );
@Argiles1
Copy link

I have paste the code in functions.php. Now, How I edit the labels of shortcodes? Thanks.

@Mosharush
Copy link
Author

Mosharush commented Jul 27, 2023

I have paste the code in functions.php. Now, How I edit the labels of shortcodes? Thanks.

Hey,
The usage of the shortcode is anywhere when you have an input fields to set text like this:

Without a specific lang (get current language automatically):

[pll_translate text="This text will translate"]

With a specific lang:

[pll_translate text="This text will translate" lang="en"]

To add optional labels is via the function above pll_tc_strings_setup, you need to define there all your strings, I put for example:

pll_register_string( 'fl-topbar-col1-text', 'Contact_Data', 'wipi', true );
pll_register_string( 'fl-arrow-breadcrumbs', 'arrow_mulilang', 'wipi', true );

After definition of the string labels, you can translate it via Polylang translation panel on WP Admin dashboard.

BTW, 'wipi' is the name of your theme text-domain, this gist originally suppose to be for Wipi theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment