0

I have a plugin which starts:

<?php
define( 'MAX_THINGS', 30 );

I would like to change the constant to be 2.

I know I can edit the plugin file directly - but that will be overwritten if the plugin is updated.

In my theme's functions.php I have:

<?php
function my_theme_setup() {
    @define( 'MAX_THINGS', 2 );

But that doesn't change things. I assume because the plugin is loaded before the theme?

Is there any way to set a constant before the plugin loads?

1
  • Can you check if the constant is always accessed directly (e.g. $something = MAX_THINGS;) or indirectly (e.g. $something = apply_filter('awesome_max_things', MAX_THINGS);)?
    – kero
    Nov 24 at 10:01

1 Answer 1

2

You won't be able to replace it at all if the plugin is not checking whether the constant has already been defined using if ( defined( 'MAX_THINGS' ) ). By definition constants can't be overwritten, so this check is necessary to make a value 'pluggable'. Adding @ to define() will suppress the error but it won't make it work.

As you've noticed, plugins load before themes, so if the plugin does include this check you will need to set the constant in either a Must-use Plugin or the wp-config.php file, since both are safely editable and load before regular plugins. In my experience if a plugin supports customisation via constants then the expectation is usually that it would be added to wp-config.php.

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.