Skip to content

Instantly share code, notes, and snippets.

@a11smiles
Last active May 13, 2025 10:53
Show Gist options
  • Select an option

  • Save a11smiles/0d6694557a06fbffb9c5bc7425561822 to your computer and use it in GitHub Desktop.

Select an option

Save a11smiles/0d6694557a06fbffb9c5bc7425561822 to your computer and use it in GitHub Desktop.

Revisions

  1. a11smiles revised this gist May 13, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function translate_shortcode( $atts = [], $content = null, $tag = '' ) {
    //display url for language
    $translated_url = $wg_url->getForLanguage( $language );

    if (!is_null($wg_url)) {
    if ( ! is_null($translated_url) ) {
    $o = '<a href="' . $translated_url . '" class="lang-link" data-wg-notranslate>' . $custom_atts['label'] . '</a>';

    set_transient(get_the_guid() . '__' . $custom_atts['lang'], $o, $cache_timeout);
  2. a11smiles revised this gist May 12, 2025. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    function translate_shortcode( $atts = [], $content = null, $tag = '' ) {
    global $wp_query;

    // Get current URI and set cache timeout
    $current_url = $_SERVER["REQUEST_URI"];
    $cache_timeout = 86400;
  3. a11smiles revised this gist May 12, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ function translate_shortcode( $atts = [], $content = null, $tag = '' ) {

    $o = null;

    // Check URL from cache if possible
    // Get URL from cache if possible
    if ( false === ( $o = get_transient( get_the_guid() . '__' . $custom_atts['lang'] ) ) ) {
    $language_services = weglot_get_service( 'Language_Service_Weglot' );
    $request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );
  4. a11smiles revised this gist May 12, 2025. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -16,10 +16,9 @@ function translate_shortcode( $atts = [], $content = null, $tag = '' ) {
    ), $atts
    );

    // Get URL from cache if possible
    $o = null;

    // Check if URL was found
    // Check URL from cache if possible
    if ( false === ( $o = get_transient( get_the_guid() . '__' . $custom_atts['lang'] ) ) ) {
    $language_services = weglot_get_service( 'Language_Service_Weglot' );
    $request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );
  5. a11smiles revised this gist May 12, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    function translate_shortcode( $atts = [], $content = null, $tag = '' ) {
    global $wp;
    global $wp_query;

    $current_url = home_url(add_query_arg(array(), $wp->request));
    // Get current URI and set cache timeout
    $current_url = $_SERVER["REQUEST_URI"];
    $cache_timeout = 86400;

    // normalize attribute keys, lowercase
  6. a11smiles revised this gist May 12, 2025. No changes.
  7. a11smiles revised this gist May 12, 2025. No changes.
  8. a11smiles created this gist May 12, 2025.
    53 changes: 53 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    function translate_shortcode( $atts = [], $content = null, $tag = '' ) {
    global $wp;
    global $wp_query;

    $current_url = home_url(add_query_arg(array(), $wp->request));
    $cache_timeout = 86400;

    // normalize attribute keys, lowercase
    $atts = array_change_key_case( (array) $atts, CASE_LOWER );

    // override default attributes with user attributes
    $custom_atts = shortcode_atts(
    array(
    'lang' => 'en',
    'label' => 'English',
    ), $atts
    );

    // Get URL from cache if possible
    $o = null;

    // Check if URL was found
    if ( false === ( $o = get_transient( get_the_guid() . '__' . $custom_atts['lang'] ) ) ) {
    $language_services = weglot_get_service( 'Language_Service_Weglot' );
    $request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );

    if ( ! is_null($language_services) && ! is_null($request_url_services) ) {
    //create url object
    $wg_url = $request_url_services->create_url_object( $current_url );

    // get a language
    $language = $language_services->get_language_from_internal($custom_atts['lang']);

    //display url for language
    $translated_url = $wg_url->getForLanguage( $language );

    if (!is_null($wg_url)) {
    $o = '<a href="' . $translated_url . '" class="lang-link" data-wg-notranslate>' . $custom_atts['label'] . '</a>';

    set_transient(get_the_guid() . '__' . $custom_atts['lang'], $o, $cache_timeout);
    }
    }
    }

    // return output
    return $o;
    }

    function translate_shortcode_init() {
    add_shortcode( 'translate', 'translate_shortcode' );
    }

    add_action( 'init', 'translate_shortcode_init' );