0

My question is very similar to this one. However, I can't add comments to get more explanation so I open a new question.

My website has no blog. The home page is a static page. But I want my category 22 to be the home page. i.e. when someone types domain.com I want them to arrive to domain.com/category/22/

The accepted answer in the question I found above, (from Chip Bennet) accomplishes what I need with this code:

function wpse121308_redirect_homepage() {
    // Check for blog posts index
    // NOT site front page, 
    // which would be is_front_page()
    if ( is_front_page() ) {
        wp_redirect( get_category_link( 22 ) );
        exit();
    }
}
add_action( 'template_redirect', 'wpse121308_redirect_homepage' );

However when this code is active in my functions.php, a user enters ANY URL to a page on a translated website, he is redirected to the English page each time. (I don't understand why that would happen but I tested it and it's really this code that does it).

My site uses this syntax for language sites: domain.com/de/

How can I change the code so that it will work also for multi-lingual sites?

1 Answer 1

1

This happens because WordPress doesn't have provide multi-lingual functionality by default. When you call get_category_link( 22 ), you'll get exactly that - the link for category with the ID of 22. WordPress isn't aware that there might be a translation for the term and it should be used instead. The multi-lingual support needs to come from somewhere, usually from a plugin, to map the given category ID to the corresponding translation's ID.

You'll need to refer to the documentation of the multi-lingual solution you're using to find out how exactly the translation mapping should be done as 3rd party plugins and themes are considered of topic here. But, here is a general example how Chip's code could be edited to support multiple languages.

function wpse121308_redirect_homepage() {
    // Check for blog posts index
    // NOT site front page, 
    // which would be is_front_page()
    if ( is_front_page() ) {
        $category_id = my_multilingual_category_id(22);
        
        wp_redirect( get_category_link( $category_id ) );
        exit();
    }
}

// Adapter function which wraps the actual translation handling function
function my_multilingual_category_id(int $term_id): int {
    // Look for the correct function to get the translated term ID
    // from the solution's documentation
    $translated_term_id = multilingual_solutions_term_translation_getter($term_id);

    // Return translated term ID, if it exists
    // use the default language term id as a fallback, if not
    return $translated_term_id ?: $term_id;
}

P.s. you'll probably need the get a local developer to help you with the debugging (if you can't do it yourself), if the redirect happens with any url and not just on the front page as finding the root cause might require more involvement than what is possible here on WPSE.

1
  • Thanks, you are right. It was actually a setting in my translations plugin that made this happen. I was searching in the wrong area for a mis-behaving redirect but the translation plugin was the culprit.
    – J.K.
    Commented Jun 24, 2023 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.