1

In the root file of my plugin (the-plugin/the-plugin.php), I've set up some defines that I will be using throughout my plugin:

# Plugin's latest version
define( 'THEPLUIGN_VERSION', '2.0.0' );
# Minimum WordPress version required
define( 'THEPLUIGN_MIN_WP', '3.7' );
# Minimum PHP version required
define( 'THEPLUIGN_MIN_PHP', '5.3' );
# Plugin base
define( 'THEPLUIGN_BASE', plugin_basename( __FILE__ ) );

As you can see, I've prefix the defines with THEPLUGIN_, I assume this is necessary to add in order to keep each of the values unique and to prevent conflict with other plugins. Is this true? Or is there a better way to define these values, globally, to be used throughout my plugin?

I've implemented both namespaces and class structure in my plugin.

  • 1
    Yes they need to be unique. I'm sure no-one else will be using THEPLUIGN ;-) – Andy Macaulay-Brook Oct 3 '16 at 19:23
  • @AndyMacaulay-Brook Thanks for the confirmation. Is this the best approach by using the define()? Or is there a better way? – Ethan Jinks O'Sullivan Oct 3 '16 at 19:24
  • If they need to be accessible in all the code files then they are certainly neater than loads of globals. It's what I do. If absolutely everything is wrapped in one class then I set a class property instead, otherwise I use constants. – Andy Macaulay-Brook Oct 3 '16 at 19:26
  • The main problem here is the global state of these constants. This is a terrible idea. – fuxia Oct 3 '16 at 20:08
  • So you wouldn't recommend using const THEPLUIGN_MIN_PHP = '5.3'; instead? What's the alternative? – Ethan Jinks O'Sullivan Oct 3 '16 at 20:22
2

Yes they do, the more interesting question is why do you use define instead of const? Take a look here: define() vs const.

The only situation in which you should use a define over const is if you want the user to be able to override your defines in wp-config.php. Obviously things like minimum php version should not be overridable.

Once you go with const you can just use namespaces and get over the whole issue. The only problem is that const before PHP 5.6 could not be assigned an expression and you have two defines which are not simple values.

If you target PHP 5.6 and above then, in my opinion, just use const. Otherwise defines should be avoided as they can be overwritten by mistake, but it is your judgment call about using defined values instead of calling the relevant APIs.

| improve this answer | |
  • So using the THEPLUIGN_BASE value that I defined in my question would need a requirements of PHP v5.6? I'd like to keep the PHP version at least 5.3 since I use namespaces and according to this usage statistic, not many are using PHP v5.6+ – Ethan Jinks O'Sullivan Oct 3 '16 at 20:35
  • Sorry, I was wrong, even 5.6 will not work there as function calls are still not accepted. maybe 7.0 not sure. The question is why do you do it in a define in the first place? If you use it as a global then all the reservations against using globals should apply. Obviously this is kinda esthetics issue here but why does the entire plugin needs to know which minimal wp version you support? only the activation function needs it, and using a global there makes it less readable and pollutes the global name space. (part of the same argument can be applied against const as well) – Mark Kaplun Oct 4 '16 at 3:58

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.