0
votes

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.

E.g. I place <?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 (e.g. header and nav).

Any ideas as to why this is happening and measures to rectify it would be greatly, greatly, appreciated!! :)

3
Are there any PHP error messages that print? Is your server perhaps set up to not print error messages? I'm thinking that maybe PHP can't find the file. Another possibility is that you're stomping on some Wordpess variables. Try adding a prefix to your variables, especially time and date. - Ryan Ballantyne
No error messages print, and my server is set-up to print error messages. I've checked the file is there manually several times over, and is being referenced by an absolute path (a relative wouldn't work as I couldn't find header.php when I went through the WP folders via FTP, plus I'd rather keep as much stuff at the top layer as possible). I'm also pretty sure it's not variable names, as I've just changed all of them. Also, when I didn't include ux_variables.php, but instead took $time = x; and $date = x; out and placed them in header.php, and attempted to echo them from a post, - Avicinnian
it didn't work, but when included in the post as variables, they echoed fine. - Avicinnian
@Phphelp - How would div alignment have anything to do with this? Not trying to be rude or harsh toned, but I don't understand how the alignment of a DIV which is really the alignment of the entire paragraph in which this echo() is contained has anything to do with it, all the strings either side of <?php echo($time); ?> in my post are rendering fine. - Avicinnian

3 Answers

0
votes

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.

0
votes

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

0
votes

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.