Created
September 27, 2017 10:48
-
-
Save Mosharush/5e69d0c0cf61333e7cfd464b471986c1 to your computer and use it in GitHub Desktop.
WordPress PolyLang translate shortcode & function - use in customizer texts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):
With a specific lang:
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: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.