PHP,Wordpress和数据库 - 哦,我的[关闭]

I am building a new WP site and am pulling data from a secondary database and displaying it within WP pages. I am currently using the PHP Insert plugin and have successfully been able to display data on my site by typing in the PHP code right into the text part of the page.

I'm new to PHP and the backend of WP sites so I'm trying to figure out if I can reference other PHP files from within my code snippet or do I have to put all the PHP code in one place.

If I can reference other PHP files, then where do I put them from within my Wordpress environment?

Any guidance you can offer for me to learn this would be greatly appreciated OR if you have a recommendation for another way to organize my new PHP code to run on WP pages instead of a plugin. THANK YOU!

I have found that, except for the initial PHP code embedded in the WordPress page, it is easiest to maintain if all the other code resides elsewhere. WP sometimes does some funny things with loading pages due to showing parts of multiple posts on the home page, etc. so I have found it safest to use require_once() instead of require() or include(). I put the files in either a web accessible directory if they are designed to also run stand-alone, or in a non-web directory (generally preferred) if they should not be accessed except as part of your WP pages. Typically that would be required_once('/home/whatever/private/xyz.php') or require_once('/var/private/xyz.php') or something like that - the specifics will depend on whether you control the entire server or are working with what a shared hosting account gives you access to use. In some cases I distill it down to just a few parameters and the rest elsewhere - e.g.:

<?php
$id = 10;
$source = 'abc';
require_once('/home/whatever/private/xyz.php');

and let xyz.php do the rest as if it were running standalone with a couple of parameters passed in. The specifics will vary, but I've done it many times successfully.