Ive built simple web applications in the past using 'include' functions and typically I've used this command:

<?php include($_SERVER["DOCUMENT_ROOT"] . "/data.php"); ?>

This has worked great OUTSIDE of WordPress but my question is - can this be applied to a WordPress Environment?

(I should add the reason I am doing this: it is because I have to load a file which is approx 2MB into a WordPress Template and it crashes each time I try to save the file).

  • using wordpress good practise is best like ` require_once ( get_template_directory() . "/data.php"); ` as @Jack Johansson is answered – Owaiz Yusufi Jul 31 '17 at 5:00

You can include the PHP files in WordPress just the same way you do it anywhere else.

However, WordPress offers more constants and functions for defining a path for the include() function.

Instead of using $_SERVER["DOCUMENT_ROOT"], move your PHP file to your theme's folder, and use this to include it:

require_once ( get_template_directory() . "/data.php"); 

The reason behind moving the file to theme's folder is both for security, and organization. Now, use this in the very first line of your data.php file:

if ( ! defined( 'ABSPATH' ) ) die();

This will prevent direct access to your PHP script by entering the URL. ABSPATH is a core WordPress constant, and if it's not defined, then it means your script is probably being accessed directly.

Also, to prevent accidental conflicts occurred by including the file multiple times, I would suggest you use require_once() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.