Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Last active February 28, 2025 09:31
Show Gist options
  • Save pavlo-bondarchuk/d7cf6fa8923ddbb3354ecc9b1cce4cbe to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/d7cf6fa8923ddbb3354ecc9b1cce4cbe to your computer and use it in GitHub Desktop.
Redirects single post URLs to their correct language-specific category URLs
<?php
add_action('template_redirect', function () {
global $post, $wp_query;
if (function_exists('wpml_get_language_information')) {
$current_lang = apply_filters('wpml_current_language', null);
if ($current_lang !== 'he') {
return;
}
$current_url = untrailingslashit(home_url(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$current_url = mb_strtolower(urldecode($current_url), 'UTF-8');
if (is_single()) {
$correct_url = untrailingslashit(get_permalink($post->ID));
$correct_url = mb_strtolower(urldecode($correct_url), 'UTF-8');
if ($current_url !== $correct_url || preg_match('/\/client_updates$/', $current_url)) {
$wp_query->set_404();
status_header(410);
get_template_part('404');
exit;
}
}
if (is_tax()) {
$term = get_queried_object();
if ($term) {
$correct_url = untrailingslashit(get_term_link($term));
$correct_url = mb_strtolower(urldecode($correct_url), 'UTF-8');
if ($current_url !== $correct_url || preg_match('/\/client_updates$/', $current_url)) {
$wp_query->set_404();
status_header(410);
get_template_part('404');
exit;
}
}
}
}
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment