{"id":46055,"date":"2023-04-23T19:51:50","date_gmt":"2023-04-23T19:51:50","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46055"},"modified":"2023-04-23T19:51:50","modified_gmt":"2023-04-23T19:51:50","slug":"custom-language-link-wpml","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/custom-language-link-wpml\/","title":{"rendered":"Custom language link WPML"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Here&#8217;s a PHP snippet that you can use to return a custom link to a specific language for the current page using WPML.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\nfunction get_custom_language_link( $lang ) {\r\n    $current_url = get_permalink();\r\n\r\n    if ( function_exists( 'icl_get_home_url' ) ) {\r\n        $translated_url = apply_filters( 'wpml_permalink', $current_url, $lang );\r\n        $translated_url = str_replace( home_url(), icl_get_home_url(), $translated_url );\r\n    } else {\r\n        $translated_url = $current_url;\r\n    }\r\n\r\n    return $translated_url;\r\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will output the custom link for the specified language code and can be used on any page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n$lang = 'fr';\n$translated_url = get_custom_language_link( $lang );\necho $translated_url;\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example 1: This will output a link that says &#8220;Switch to French&#8221; and links to the translated version of the current page in French.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\r\n$lang = 'fr'; \/\/ replace with the language code of your desired language\r\n$translated_url = get_custom_language_link( $lang );\r\n?&gt;\r\n&lt;a href=\"&lt;?php echo $translated_url; ?&gt;\"&gt;Switch to French&lt;\/a&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example 2: This will output a list of links for English and French, wrapped in an unordered list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\r\n$en_url = get_custom_language_link( 'en' );\r\n$fr_url = get_custom_language_link( 'fr' );\r\n?&gt;\r\n&lt;ul class=\"language-switcher\"&gt;\r\n  &lt;li&gt;&lt;a href=\"&lt;?php echo $en_url; ?&gt;\"&gt;EN&lt;\/a&gt;&lt;\/li&gt;\r\n  &lt;li&gt;&lt;a href=\"&lt;?php echo $fr_url; ?&gt;\"&gt;FR&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;\/ul&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a PHP snippet that you can use to return a custom link to a specific language for the current page using WPML. This will output the custom link for the specified language code and can be used on any page. Example 1: This will output a link that says &#8220;Switch to French&#8221; and links [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":46582,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Custom language link WPML - Code DPlugins","description":"Here's a PHP snippet that you can use to return a custom link to a specific language for the current page using WPML. This will output the custom link for the s"},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[7,3],"tags":[92],"dpc_library":[],"class_list":["post-46055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oxygen","category-wordpress","tag-wpml"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46055"}],"version-history":[{"count":0,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46055\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46582"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46055"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46055"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}