0

Is there a way to call functions from a default location for multiple installed sites on the same server?

For example:

\public_html\functions\global_functions.php has the shared functions for all sites.

\public_html\site1\wp-content\themes\theme1_child\functions.php 
require_once( dirname(__FILE__) . '../../../../global_functions.php'); 

\public_html\site2\wp-content\themes\theme1_child\functions.php 
require_once( dirname(__FILE__) . '../../../../global_functions.php'); 

I know this is not correct but I am not sure if it is even possible to do something like this. Since it is on the same server, I didn't know if there was a way to see other directories above the current install point.


After continuing to play with this, I ended up doing it this way:

define ( 'global_functions_dir', dirname(__FILE__) . '../../../../../_functions' );

require_once( global_functions_dir . '/global-functions.php');

which can be added on each site to include the necessary functions. If anyone knows if there are any security risks doing it this way, please let me know. Since this is a server-side call, I would think this is fine??

I have started reading through the multi-site configuration and this could be a great way to accomplish this. I will look at moving in this direction after I have a better understanding of the issues that might arise by doing so. Thank you for your help.

1
  • If your question has been answered, could you please accept the solution. Thanks. Commented Jan 4, 2017 at 20:11

1 Answer 1

0

Your include path is incorrect.

You have:

require_once( dirname(__FILE__) . '../../../../global_functions.php');

When it should be:

require_once( dirname(__FILE__) . '../../../../functions/global_functions.php');


The other alternative would be to change your setup from individual standalone installations of WordPress to WordPress Multi-site.

You would then have a couple of options:

  1. Option #1 being a Must-Use Plugin.
  2. Option #2 being a standard Plugin (Activated network-wide).
  3. Option #3 being a standard Plugin (Activated on a per-site basis).

Personally, I would recommend option #1 (Must-Use plugin). This will activate the plugin across every site in your network and it can't be deactivated or removed via user interface. This guarantees the code will be available for all sites in your network at all times.

Option #2 is nice, because it does exactly the same as option #1, except if you want to troubleshoot what might be causing a problem, you can very easily activate or deactivate the functionality with a simple click through the administrative user interface.

Option #3 is a bit more versatile. You can activate the plugin on a per-site basis. Maybe you have three sites to manage, but only two of them need the functionality. Then you can activate it on just those two sites and not the third one.

Either way, which ever option you choose (related to WordPress multi-site), you only have to manage the functionality in one place, while multiple sites can share and use that functionality.

7
  • It it safe to be using a relative URL like that, though? Commented Jan 1, 2017 at 16:42
  • I updated my answer. I would recommend using WordPress Multi-site. Commented Jan 1, 2017 at 16:54
  • I like not having to rely on plugins in case something changes and the plugin does not get updated. I would rather use the direct path but I also do not want this to cause a security risk. Since this is all server-side, does it pose a risk?
    – Jeronimo
    Commented Jan 1, 2017 at 19:01
  • It would be you maintaining the available functionality. Isn't that what you're currently doing? Managing it through WordPress multi-site would result in better performance, organization and likely better security. Commented Jan 1, 2017 at 19:15
  • I have never used the Multi-site feature before so I am not sure what I would be getting myself in to. I currently have 6 sites running, each with its own separate database. 4 of the sites are similar in structure and can use the same functions. I thought that if I created a "global" location for functions and included them in each site, this would cut down tremendously on management. If switching the Multi is not difficult or would not cause any problems to existing sites, I might consider it but if this does not cause a security issue, I don't have a problem with this solution.
    – Jeronimo
    Commented Jan 1, 2017 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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