0

I want my plugin to activate if the WP and php version in the the plugin header are at the required version or greater.

However, WordPress automatically calls wp_die() if the site does not meet the min. I rather use an admin notice, anything less harsh than wp_die(), but I can't find any info on how WordPress automatically calls wp_die() and is there is a filter to do something else.

3
  • I don't understand your question, can you explain what you meant in the first sentence? It's not possible to change the version of PHP or WordPress that runs by changing the plugin header, I must have misunderstood. What is the problem that you are trying to solve that requires this?
    – Tom J Nowell
    May 18, 2021 at 20:13
  • WordPress does a version check based on the plugin header and calls wp_die() if the site does not meet the requirements. I want to use the header to control the min version, with out wp_die(0 when the version is lower. May 18, 2021 at 21:07
  • that suggests your plugin would be capable of running on that version of WordPress/PHP which also suggests your plugins PHP/WP value are too high and inaccurate. The point of these values is that if you specify WP 5.0 it's because your plugin will be broken on 4.9 and lower.
    – Tom J Nowell
    May 18, 2021 at 22:29

1 Answer 1

1

However, WordPress automatically calls wp_die() if the site does not meet the min. I rather use an admin notice, anything less harsh than wp_die(), but I can't find any info on how WordPress automatically calls wp_die() and is there is a filter to do something else.

This is not possible via the plugin header.

The reason is because to implement this you need to run PHP code, but WordPress does not run your plugins code to get this information. It loads the first few bytes of the main plugin file and manually searches through the result for the plugin header.

Otherwise, if it ran the PHP file to get the value, and you used a PHP feature that wasn't available, the site would crash every time you loaded the plugins page or did an update check.

This also means that even if there was a filter to change this behaviour, your plugin has no way to use it. You would need a second plugin that had already been activated to add the filter and change the behaviour.

You can get halfway by lying in the plugin header to set a very low minimum value, then manually perform the check yourself, however:

  • The plugin directory and the plugin page will both report that it is compatible, even though it is not
  • you will need to re-architect your plugin so that none of your actual code is in the main file, only the PHP/WP version checker, you'll need to conditionally load files based on the result
  • plugin activation failure is used in lots of checks that are now no longer functional
  • the end user may not realise your plugin is in a zombie/limbo mode as most WP Admin screens have a lot of notices
  • such a plugin would be guaranteed to be rejected by the .org plugin review team, and would be seen as a major red flag to lots of enterprise companies and agencies, if not more

Overall, this is a terrible idea with poor UX, and lots of maintenance and practical consequences. The only way to do this is to lie in your plugin header, but even that will cause problems.

7
  • But it does work just fine . . . I have a WP site version 5.6 and if I put: Requires at least: 5.7 in the plugin header plugin with zero plugin code other then the header wp_die() is triggered, but if I bump it to 5.7 the plugin activates. May 18, 2021 at 22:59
  • Is looks like in version 5.2 it was added see track ticket: 43992 "Allow for plugins not in Plugin Directory but containing plugin headers" and why the plugin header is being read for version control. May 18, 2021 at 23:07
  • I don;'t need a second plugin to run code because the plugin activation hook runs and form there I can do just about anything I need including triggering an admin notice, or a popup or whatever to let them know they are on the wrong version. For example, I am already triggering an admin notice regardless of the version. May 18, 2021 at 23:09
  • If it's a terrible idea and poor UX, let WordPress know, I am just plying by there rules. May 18, 2021 at 23:12
  • 1
    I don't understand the english in most of your comments, but as I said, core does not run/load your plugin to get the plugin header, it reads it in like a text file, it does not run/execute it like a PHP file, no code is executed before the plugin is activated, so there is nothing you can do in PHP until the plugin has already been activated, which shouldn't happen if your PHP/WP version is not high enough. It does not matter if your code works, you have no way of running it, the plugin activation hook can't run
    – Tom J Nowell
    May 18, 2021 at 23:57

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.