<?php
add_filter( 'rank_math/frontend/canonical', '__return_false' );


add_action('wp_head', 'custom_seo_tags_for_vivn', 1);
function custom_seo_tags_for_vivn() {
    $current_url = $_SERVER['REQUEST_URI'];
    if (is_front_page() || strpos($current_url, '/vi-vn/') !== false) {
        $domain = 'https://' . $_SERVER['HTTP_HOST'];
        echo "\n"; 
        echo "\t" . '<link rel="shortlink" href="' . $domain . '/" />' . "\n"; 
        echo "\t" . '<link rel="canonical" href="' . $domain . '/vi-vn/" />' . "\n";
        echo "\t" . '<link rel="alternate" hreflang="vi" href="' . $domain . '/vi-vn/" />' . "\n";
        echo "\t" . '<link rel="alternate" hreflang="vi-vn" href="' . $domain . '/vi-vn/" />' . "\n";
    }
}


add_filter( 'request', 'custom_vivn_routing_fix' );
function custom_vivn_routing_fix( $query_vars ) {
    if ( strpos($_SERVER['REQUEST_URI'], '/vi-vn/') !== false ) {
        $query_vars = array();
        if ( 'page' == get_option( 'show_on_front' ) ) {
            $query_vars['page_id'] = get_option( 'page_on_front' );
        }
    }
    return $query_vars;
}


add_filter( 'redirect_canonical', 'disable_vivn_canonical_redirect', 10, 2 );
function disable_vivn_canonical_redirect( $redirect_url, $requested_url ) {
    if ( strpos( $_SERVER['REQUEST_URI'], '/vi-vn/' ) !== false ) {
        return false; // Trả về false để chặn hoàn toàn lệnh đổi URL của WordPress
    }
    return $redirect_url;
}

require get_template_directory() . '/inc/init.php';

if (function_exists('flatsome')) {
    flatsome()->init();
}





