[WordPress] wp_redirect()関数が動作していない場合は、

Last Updated:2023年07月17日| | コメントを残す

wp_redirect()関数は、headerが送信される前にロードする必要がありそうですね(wp_redirect()の詳細については、 WordPress Codex文書を参照してください)。 だから:

<?php
/**
* .. Template 주석
*/

get_header();

if(...) {
...
if(...) {
...
wp_redirect($url);
exit();
}
}
?>

上記のようにheader()の後に位置させると、正常に動作せず、ヘッダ部分をwp_redirect()ブロックの下に移動するときオン動作します。

<?php
/**
* .. Template 주석
*/
if(...) {
...
if(...) {
...
wp_redirect($url);
exit();
}
}
get_header();
?>
// 참고: http://wordpress.stackexchange.com

そして、次のようなコードの場合にも、正常に動作しません。

echo '<script>alert("set thumbnail")';
$location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
wp_safe_redirect( $location );
echo '<script>alert("End Loop");</script>';

この場合、echo部分を削除すると、正常に動作します(参照).

wp_redirect()は、テンプレート内では、header()が既に送信されたために使用するのには遅すぎるので、 template redirect アクションなどのフックを使用して、より早くチェックする必要があります。

add_action( 'template_redirect', 'wpse52455_redirect' );

function wpse52455_redirect(){
// 여기에서 체크하여 wp_redirect 호출하기
}
// 참고: stackexchange

"Headers Already Sent「メッセージが表示された場合 この記事を参考にしてみてください。

メモ:


コメントを残す

コメント