Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include a PHP file in Wordpress that is outside of my theme but on the same server?

Tags:

php

wordpress

Wordpress is installed in a directory called 'blog' on my website and it uses a custom theme:

mysite.com/blog/wp-content/themes/mytheme/

I would like to alter the theme so that it calls the same header and footer as the rest of my site, which are in a folder off the root directory of my site called 'include':

mysite.com/include/header.php

Is this possible?

At the moment, my custom theme has a complete copy of the header and footer saved in it's local directory (along with the relevant CSS, JS etc.) but it is becoming a real pain having to maintain two identical copies.

These copies are referenced in the theme using ABSPATH, but this seems to have limited scope as the furthest back I have been able to get is to the /blog/ directory. If I replace ABSPATH with an absolute URL, I get an error about security.

Any help or advice would be greatly appreciated.

like image 561
sixoftwelve Avatar asked Oct 22 '25 17:10

sixoftwelve


1 Answers

As per Thomas's suggestion, this worked perfectly (source: https://css-tricks.com/php-include-from-root/)

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/include/header.php";
include_once($path);
like image 164
sixoftwelve Avatar answered Oct 24 '25 07:10

sixoftwelve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!