What’s the best way to ‘include’ a file in WordPress?

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.