내가 원하는 것은 내 PHP 스크립트 중 하나를 Word Press 테마에 포함시키는 것입니다. 문제는 스크립트 파일에 포함 된 변수에 테마 파일의 함수 안에 액세스 할 수없는 스크립트 파일을 포함 시킨다는 것입니다.
테마 폴더에 새 파일을 만들고 header.php와 동일한 코드를 추가했습니다.이 파일을 열면 잘 작동합니다. 그래서 내가 말할 수있는 한, Word Press와 관련된 것입니다. 의
$global_var = 'global';
print_r($GLOBALS); // if I open this file directly this prints globals WITH $global_var;
// if this file is included in header this prints all the WP stuff WITHOUT $global_var;
내용 /var/www/vhosts/domain/include_file.php :의
/other/path/wordpress/wp-content/themes/theme-name/header.php // this is broken
/other/path/wordpress/wp-content/themes/theme-name/test.php // this works
/var/www/vhosts/domain/wordpress/ ->(symlink)-> /other/path/wordpress/
/other/path/wordpress/wp-content/themes/theme-name/header.php
/var/www/vhosts/domain/include_file.php
내용/기타/경로/워드 프레스/WP - 콘텐츠/테마/테마 - 이름/헤더 .php require '/path/to/include_file.php';
print $global_var; // this prints 'global' as expected
function test()
{
global $global_var;
print $global_var; // this is NULL
}
test();
print_r($GLOBALS); // this prints all the WP stuff WITHOUT $global_var in it
전역 변수가 좋지 않으므로 사용하지 마세요. – silent
자동 로더 역할을하며 자동으로 매개 변수를받는 함수에 사용해야합니다. global은 데이터를 가져 오는 가장 간단한 방법이었습니다. – Brayn