送信時にCookieを保存するフォームを使用して、2言語のWebサイトを作成しました。各ページは、Cookieをチェックして、ロードする言語を確認します。
私が抱えている問題は、ページを読み込んで言語を切り替えるために、送信ボタンを2回押す必要があることです。
これは私が持っているフォームです:
<form action="<?php the_permalink(); ?>" name="region" method="post">
<input type="submit" name="region" value="English" id="en-button" />
<input type="submit" name="region" value="Cymraeg" id="cy-button" />
</form>
これは、Cookieを保存するためのfunctions.phpファイルにあります。
function set_region_cookie()
{
if(isset($_POST['region']))
{
// Set Cookie
setcookie('region', $_POST['region'], time()+1209600);
// Reload the current page so that the cookie is sent with the request
header('Region: '.$_SERVER['REQUEST_URI']);
}
}
add_action('init', 'set_region_cookie');
そして、これは私がさまざまなコンテンツをロードするために各コンテンツ領域の周りに持っているものです:
<?php $language = $_COOKIE["region"];
if ($language == "English") { ?>
<?php echo the_field('english_content'); ?>
<?php } else { ?>
<?php echo the_field('welsh_content'); ?>
<?php } ?>
言語は正しく切り替わりますが、送信ボタンを2回クリックした場合に限ります。