Last updated on October 18, 2023

WPML language switcher exclude pages

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Exclude specific pages from WPML language switcher.

The code snippet provided below allows you to exclude specific pages from the WPML language switcher in WordPress. This can be useful if you have certain pages that you don’t want to be translated or displayed in the language switcher.

function wpsnippets_exclude_pages_from_language_switcher( $args ) {
    $args['exclude'] = array( 1, 2, 3 ); // Replace with the IDs of the pages you want to exclude
    return $args;
}
add_filter( 'wpml_ls_pages', 'wpsnippets_exclude_pages_from_language_switcher' );

To use this code snippet, you need to add it to your theme’s functions.php file or a custom plugin file.

In the code snippet, the wpsnippets_exclude_pages_from_language_switcher function is a custom PHP function that is hooked into the wpml_ls_pages filter. This filter allows you to modify the list of pages displayed in the language switcher.

Inside the function, we set the exclude parameter of the $args array to an array of page IDs that you want to exclude from the language switcher. You can replace the array( 1, 2, 3 ) with the actual page IDs you want to exclude.

Finally, the modified $args array is returned, which will be used by WPML to generate the language switcher.

Remember to replace the page IDs in the code snippet with the actual IDs of the pages you want to exclude.

Examples

Example 1: Exclude specific pages from WPML language switcher

This example demonstrates how to exclude specific pages from the WPML language switcher using the icl_ls_languages filter hook.

function wpsnippets_exclude_pages_from_language_switcher( $languages ) {
    $excluded_pages = array( 2, 5, 8 ); // IDs of the pages to exclude

    foreach ( $languages as $language_code => $language ) {
        $languages[ $language_code ]['url'] = remove_query_arg( 'lang', $language['url'] );

        foreach ( $excluded_pages as $page_id ) {
            $languages[ $language_code ]['url'] = add_query_arg( 'exclude_page', $page_id, $languages[ $language_code ]['url'] );
        }
    }

    return $languages;
}
add_filter( 'icl_ls_languages', 'wpsnippets_exclude_pages_from_language_switcher' );

This code example uses the icl_ls_languages filter hook to modify the language switcher URLs. It excludes specific pages (identified by their IDs) from the language switcher by adding a query parameter exclude_page to the URL. The remove_query_arg() function is used to remove the lang query parameter, and the add_query_arg() function is used to add the exclude_page query parameter.

Example 2: Exclude child pages from WPML language switcher

This example demonstrates how to exclude child pages of a specific parent page from the WPML language switcher using the icl_ls_languages filter hook.

function wpsnippets_exclude_child_pages_from_language_switcher( $languages ) {
    $parent_page_id = 10; // ID of the parent page
    $excluded_pages = get_pages( array( 'child_of' => $parent_page_id ) ); // Get child pages

    foreach ( $languages as $language_code => $language ) {
        $languages[ $language_code ]['url'] = remove_query_arg( 'lang', $language['url'] );

        foreach ( $excluded_pages as $page ) {
            $languages[ $language_code ]['url'] = add_query_arg( 'exclude_page', $page->ID, $languages[ $language_code ]['url'] );
        }
    }

    return $languages;
}
add_filter( 'icl_ls_languages', 'wpsnippets_exclude_child_pages_from_language_switcher' );

This code example uses the icl_ls_languages filter hook to modify the language switcher URLs. It excludes child pages of a specific parent page from the language switcher by adding a query parameter exclude_page to the URL. The get_pages() function is used to retrieve the child pages, and the remove_query_arg() and add_query_arg() functions are used to manipulate the URLs.

Example 3: Exclude pages based on custom criteria from WPML language switcher

This example demonstrates how to exclude pages from the WPML language switcher based on custom criteria using the icl_ls_languages filter hook.

function wpsnippets_exclude_pages_based_on_criteria( $languages ) {
    $excluded_pages = array(); // Array to store excluded page IDs

    foreach ( $languages as $language_code => $language ) {
        $languages[ $language_code ]['url'] = remove_query_arg( 'lang', $language['url'] );

        // Custom criteria to exclude pages
        if ( is_page_template( 'template-custom.php' ) ) {
            $excluded_pages[] = get_the_ID();
        }
    }

    foreach ( $excluded_pages as $page_id ) {
        foreach ( $languages as $language_code => $language ) {
            $languages[ $language_code ]['url'] = add_query_arg( 'exclude_page', $page_id, $languages[ $language_code ]['url'] );
        }
    }

    return $languages;
}
add_filter( 'icl_ls_languages', 'wpsnippets_exclude_pages_based_on_criteria' );

This code example uses the icl_ls_languages filter hook to modify the language switcher URLs. It excludes pages from the language switcher based on custom criteria. In this example, pages using a specific custom template (template-custom.php) are excluded. The is_page_template() function is used to check the page template, and the get_the_ID(), remove_query_arg(), and add_query_arg() functions are used to manipulate the URLs.

Last updated on October 18, 2023. Originally posted on December 10, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *