简体   繁体   中英

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:

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).

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.

Eg I place <?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. 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. 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.

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.

Try to debug it this way. Put the codes /absolute/path/ue_variables.php inline into the header.php , stripped it to just:

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. 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. Instead WordPress uses hooks and built in functions. 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. 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. 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'); Hopefully this helps, if your not using a child theme this might work. Either way let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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