Skip Navigation
availability:

WPML Version: 3.2

description:

Returns a post type link or taxonomy term link in the current language.

* Note: There are alternative ways to retrieve an element’s link which gives developers fuller control over the HTML output.
WordPress offers a number of functions to do this such as get_permalink(), get_term_link(), etc.

If you plan on using functions that require a post type or term ID as a parameter then you may be interested in looking into the wpml_object_id filter.

type:
filter
category:
Retrieving Localized Content
parameters:
apply_filters( 'wpml_element_link', int $element_id, string $element_type, string $link_text, array $optional_parameters, string $anchor, bool $echo, bool $return_original_if_missing )
$element_id
(int) (Required) The ID of the post type (post, page, attachment, custom post) or taxonomy term (tag, category, custom taxonomy) to link to
$element_type
(string) (Optional) The type of element to link to. Can be ‘post’, ‘page’, ‘attachment’, ‘{custom post}’, ‘tag’, ‘category’, ‘{custom taxonomy}’. Defaults to ‘post’
$link_text
(string) (Optional) The link text. Defaults to the element’s name
$optional_parameters
(array) (Optional) Arguments for the link
$anchor
(string) (Optional) Anchor for the link
$echo
(bool) (Optional) 0|false to return or 1|true to echo the localized link. Defaults to true
$return_original_if_missing
(bool) (Optional) If set to true it will always return a value (the original value, if translation is missing). Default is TRUE
hook example usage:

Examples

//produces: <a href="/hello-world/">Hello World!</a>
apply_filters( 'wpml_element_link', 1 );

//produces: <a href="/sample-page/">Custom link text for the sample page</a>
apply_filters( 'wpml_element_link', 2, 'page', __( 'Custom link text for the sample page' ) );

//produces: <a href="/sample-page/?category=foo&bar=baz">Sample Page</a>
apply_filters( 'wpml_element_link', 2, 'page', '', array( 'category'=>'foo', 'bar'=>'baz' ) );

//produces: <a href="/sample-page/#contact">Sample Page</a>
apply_filters( 'wpml_element_link', 2, 'page', '', '', 'contact' );

//produces: <a href="/category/uncategorized/">Uncategorized</a>
apply_filters( 'wpml_element_link', 1, 'category' );

//produces: <a href="/?attachment_id=25">'Attachment image title</a>
apply_filters( 'wpml_element_link', 25, 'attachment', __( 'Attachment image title' ) );