【发布时间】:2017-11-04 21:08:54
【问题描述】:
我需要检查用户是否已登录,根据结果,我希望将用户重定向到自定义字段 A 或 B。这是我目前的代码:
<?php
global $current_user;
get_currentuserinfo();
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-config.php');
add_action('get_header', 'redirect');
function redirect () {
global $post;
if (is_page() || is_object($post)) {
if (get_post_meta($post->ID, 'redirect', true)) {
header('Location: ' . get_post_meta($post->ID, 'redirect', true));
}
}
}
function redirect_b () {
global $post;
if (is_page() || is_object($post)) {
if (get_post_meta($post->ID, 'Shortlink', true)) {
header('Location: ' . get_post_meta($post->ID, 'Shortlink', true));
}
}
}
if ($current_user->ID == '') {
redirect();
}
else {
redirect_b();
}
?>
它不起作用,每当我激活它时都会收到错误 500。谁能帮我?非常感谢!
【问题讨论】:
-
在
header调用之后添加exit;,在调用重定向之前不要渲染任何东西。 -
下面的答案很好,但供您参考,此代码永远不会评估为真。如果当前用户未登录,则当前用户 id 为 0 if ($current_user->ID == '')