簡體   English   中英

is_user_logged_in 在 wordpress 目錄之外不起作用

[英]is_user_logged_in is not working outside of wordpress directory

我似乎無法讓 is_user_logged_in 在我的 wordpress 目錄之外工作。

我在 mydomain/news/ 中安裝了 wordpress

如果我執行例如 mydomain/test.htm 並在 html 中有一個 iframe,其中源位於 mydomain/news 中,則該 iframe 中的以下代碼有效:

<?php
session_start();
define( 'WP_USE_THEMES', false ); 
Include_once($_SERVER['DOCUMENT_ROOT'] . '/news/wp-load.php');
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$username=$current_user->user_login;
}
?>

但我需要從 mydomain/test.htm 中執行測試,而不是從 mydomain/news 中的 iframe 中執行測試

當我直接在 mydomain/test.htm 中嘗試以下代碼時,它不會返回 $username - is_user_logged_in() 返回 false。

<?php
session_start();
define( 'WP_USE_THEMES', false ); 
Include_once($_SERVER['DOCUMENT_ROOT'] . '/news/wp-load.php');
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$username=$current_user->user_login;
}
?>

有誰知道為什么我沒有讓 is_user_logged_in() 返回 true? - 執行測試代碼時我肯定已登錄。

您提到: mydomain/test.htm ,因此除非您的服務器設置為通過 PHP 執行.htm ,否則它將無法正常工作,您需要將文件格式更改為.php

我找到的解決方案是將以下代碼添加到 wp-config.php:

define('ADMIN_COOKIE_PATH', '/wp/wp-admin');
define('COOKIE_DOMAIN', '');
define('COOKIEPATH', '');
define('SITECOOKIEPATH', '');

注意這段代碼要加在wp-config.php的中間,即在以下行之前: /* That's all, stop editing! Happy blogging. */ /* That's all, stop editing! Happy blogging. */ /* That's all, stop editing! Happy blogging. */ 您應該根據您的 wordpress 安裝位置替換“/wp/wp-admin”(在此示例中,主路徑位於 /wp,並且有一個子文件夾 /wp/wp-admin)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM