简体   繁体   English

在WordPress header.php中包含自定义PHP-页面无法加载

[英]Including custom PHP in WordPress header.php - page doesn't load

I've been trying to sort this out for the past half hour. 在过去的半小时内,我一直在努力解决这一问题。

I have a file, ue_variables.php which I want to include throughout the WordPress theme, the contents of the file is: 我有一个文件ue_variables.php ,我希望将其包含在整个WordPress主题中,该文件的内容为:

if(!empty($_SERVER['HTTP_REFERER'])) {
    $url_referrer = $_SERVER['HTTP_REFERER']; 
    $explode = explode(".",$url_referrer);  
    $name_referrer = ucfirst($explode);
} else {
    $url_referrer = "default";
    $name_referrer = "default";
}

date_default_timezone_set("UTC");
$time = date("H:i:s");
$date = date("j F Y");

I want to include it in header.php so that the user can echo variables in a post (they've already got the Exec-PHP plugin setup, so that itself isn't a problem). 我想将其包含在header.php以便用户可以在帖子中回显变量(他们已经安装了Exec-PHP插件,因此本身不是问题)。

First I was being stupid about it, attempting to include it from the WordPress root folder (which doesn't even contain header.php ), then I swapped out the false relative link for a true absolute one, and now my WordPress page is breaking, from the point of inclusion downwards. 首先,我对此很愚蠢,尝试从WordPress根文件夹(甚至不包含header.php )中包含它,然后我将错误的相对链接换成一个真正的绝对链接,现在我的WordPress页面正在断开,从包容性的角度向下。

Eg I place <?php include("/absolute/path/ue_variables.php"); ?> 例如,我放置<?php include("/absolute/path/ue_variables.php"); ?> <?php include("/absolute/path/ue_variables.php"); ?> at the top of header.php and the whole index page wont load. header.php顶部的<?php include("/absolute/path/ue_variables.php"); ?>不会加载整个索引页。 I place it at the bottom, and only the header section of the blog loads (eg header and nav). 我将其放置在底部,并且仅加载博客的标头部分(例如标头和导航)。

Any ideas as to why this is happening and measures to rectify it would be greatly, greatly, appreciated!! 任何关于为什么发生这种情况的想法以及纠正它的措施将不胜感激! :) :)

You don't have a opening <?php tag so when it includes it, it doesn't know what type of code it is and likely fails. 您没有开放的<?php标记,因此当包含它时,它不知道它是什么类型的代码,并且很可能会失败。 If you plan on including a separate file, this is necessary to add an opening php tag (but not a closing), otherwise add opening and closing php tags around the code and place directly in the header.php file located in your theme folder. 如果计划包括一个单独的文件,则有必要添加一个开头的php标签(而不是一个结尾),否则,在代码周围添加开头和结尾的php标签,并直接放置在主题文件夹中的header.php文件中。

Any error messages? 有任何错误讯息吗? When you put it at the top, the whole page won't load; 当您将其放在顶部时,整个页面将不会加载; when put at the bottom, the page load, but the script won't load, at first hand, I think /absolute/path/ue_variables.php could contain errors. 当放在底部时,页面加载,但是脚本不会加载,第一手,我认为/absolute/path/ue_variables.php可能包含错误。

Try to debug it this way. 尝试以这种方式调试它。 Put the codes /absolute/path/ue_variables.php inline into the header.php , stripped it to just: 将代码/absolute/path/ue_variables.php内联放入header.php ,将其剥离为:

date_default_timezone_set("UTC");
$time = date("H:i:s");
$date = date("j F Y");

Test it. 测试一下。 If it works, then put the if-else statements. 如果可行,则放置if-else语句。 Strip down the code block by block to narrow down to where the error could be appearing from. 逐个代码地缩小代码范围以缩小到可能出现错误的位置。

It's my understanding that WordPress doesn't use the conventional includes to include script. 据我了解,WordPress不使用常规的include来包含脚本。 Instead WordPress uses hooks and built in functions. 相反,WordPress使用挂钩和内置函数。 I'm new to wordpress myself so I can't help you a ton but I can kind of point you in the right direction. 我是不熟悉Wordpress的人,因此我无法为您提供大量帮助,但我可以为您指明正确的方向。 Are you using a child theme or a parent them? 您正在使用儿童主题还是父母主题? Because if your using a child theme you could make a copy of the header file and rename it to something like header-pain. 因为如果您使用子主题,则可以制作头文件的副本并将其重命名为头疼痛。 Then you make copies of the page files that you want to include your new header and find the header() functions. 然后,复制要包含新标题的页面文件,并找到header()函数。 You then would pass these functions the parameter of your new header. 然后,您可以将新标头的参数传递给这些函数。 So in this example those functions would look like this get_header('pain'); 因此,在此示例中,这些函数看起来像这样:get_header('pain'); Hopefully this helps, if your not using a child theme this might work. 希望这会有所帮助,如果您不使用儿童主题,则可能会起作用。 Either way let me know 无论哪种方式,我都知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM