前提
function.php について
front-page で cookie をセットして、持っていない人がトップページ以外にアクセスしたらトップページに飛ばされる仕組みを作っています。
条件分岐で is_front_page が認識されなくて困っています。
実現したいこと
is_front_page を認識したい
発生している問題・エラーメッセージ
なし
該当のソースコード
php
1function init_session_start() { 2 if (!is_admin()) { 3 if (is_front_page() && !isset($_COOKIE['pass'])) { // フロントページとして認識されない 4 $value = "gYEb7cGSR6tnIoBxs6dfXv30v"; 5 setcookie("pass", $value, time() + 3600); //一週間 6 console_log($value); 7 } 8 if (!is_front_page() && !isset($_COOKIE['pass'])) { // フロントページでも作動してしまう 9 header('Location: ' . 'https://xxx.jp/', true, 301); 10 exit; 11 } 12 } 13} 14add_action('after_setup_theme', 'init_session_start');
使っているテンプレートは「front-page.php」です。
試したこと
php
1// get current template 2add_filter('template_include', 'var_template_include', 1000); 3function var_template_include($t) { 4 $GLOBALS['current_theme_template'] = basename($t); 5 return $t; 6} 7function get_current_template($echo = false) { 8 if (!isset($GLOBALS['current_theme_template'])) { 9 return false; 10 }; 11 if ($echo) { 12 echo $GLOBALS['current_theme_template']; 13 } else { 14 return $GLOBALS['current_theme_template']; 15 }; 16}; 17 18// get current url 19function get_current_url() { 20 return home_url($_SERVER['REQUEST_URI']); 21} 22 23if(get_current_template() == 'front-page.php') {} 24// 認識されず 25 26if (get_current_url() == home_url() . '/' && !isset($_COOKIE['pass'])) { 27 $value = "gYEb7cGSR6tnIoBxs6dfXv30v"; 28 setcookie("pass", $value, time() + 3600); //一週間 29 console_log($value); 30 } 31 if (!get_current_url() == home_url() . '/' && !isset($_COOKIE['pass'])) { 32 header('Location: ' . 'https://xxx.jp/', true, 301); 33 exit; 34 } 35// リダイレクトされない
正直お手上げ状態です。どうすれば思ったような実装ができるでしょうか。どうかお力添えいただけないでしょうか。
補足情報(FW/ツールのバージョンなど)
Firefox 最新版

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/22 04:10