I have a program that can run standalone (outside WP) or inside WP. If the program is running 'inside' WP (via a custom template using appropriate enqueue scripts and add_actions), then there is a constant that the program needs to be defined - an email address that the standalone program will use.
If the program is running on a WP site (inside WP), then the program needs to use the WP admin email address (via the get_option( 'admin_email' )
function.
If the program is standalone (on a non-WP site), then there is a different process that is used to set the email address used by the program.
There are many constants defined within wp-config.php
and wp-settings.php
. What is a good constant to use to check if WP is running? For instance, an option is ABSPATH.
I am looking for a 'best practice' of which WP constant to check - and ideally would be a constant that is not likely to be used in a non-WP site (although I understand that this is impossible to guarantee).
I have a program that can run standalone (outside WP) or inside WP. If the program is running 'inside' WP (via a custom template using appropriate enqueue scripts and add_actions), then there is a constant that the program needs to be defined - an email address that the standalone program will use.
If the program is running on a WP site (inside WP), then the program needs to use the WP admin email address (via the get_option( 'admin_email' )
function.
If the program is standalone (on a non-WP site), then there is a different process that is used to set the email address used by the program.
There are many constants defined within wp-config.php
and wp-settings.php
. What is a good constant to use to check if WP is running? For instance, an option is ABSPATH.
I am looking for a 'best practice' of which WP constant to check - and ideally would be a constant that is not likely to be used in a non-WP site (although I understand that this is impossible to guarantee).
Share Improve this question asked Feb 16, 2022 at 20:03 Rick HellewellRick Hellewell 7,1062 gold badges22 silver badges41 bronze badges 1 |2 Answers
Reset to default 2I don't know if others CMS or system may use ABSPATH or not but it's seems to be a generic words.
I think I would check for a only-WP constant as WPINC
or check if a WordPress function or Class exists
Based on the other answer, I decided that looking for a specific WP function is the best approach to determine if my code is running in WP or not.
I used this code block:
if (function_exists('get_bloginfo')) { // most likely a WordPress function
if (get_bloginfo('admin_email')) { // set it only if there is a value (a double-check)
$admin_email = get_bloginfo('admin_email');
}
}
It first checks if the get_bloginfo
function exists, because I need to let a prior setting of $admin_email 'stand' if not running withing WP.
The second line checks for a value for the 'admin_email', which will be set in any WP installation. This is to handle any chance that there is a get_bloginfo()
in a non-WP site. There might be a get_bloginfo()
in the non-WP site, but even less a possibility that there is a 'admin_email' setting that will be available on a non-WP site.
The next line sets that variable.
This is probably not a perfect solution, but I think it handles enough possibilities that it is 'good enough'. With this code, I can get the WP site's admin-email, if the application is running on a WP site. Otherwise, I use the value previously set in the program.
if ( class_exists( 'WP_Post' ) ) { ... }
, too, sinceWP_Post
is a core WordPress class. – Pat J Commented Feb 18, 2022 at 19:47