WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 4 months ago

#33215 new defect (bug)

Main plugin file loaded multiple times under special circumstances on Network Activate

Reported by: ccprog Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.3
Component: Plugins Keywords: needs-patch
Focuses: administration, multisite Cc:
PR Number:

Description

Consider the following code in the main plugin file:

function test_plugin_install ( $network_wide = null ) {
    if ( $network_wide ) {
        trigger_error( 'Please activate individually.', E_USER_ERROR );
    }
}
register_activation_hook( __FILE__, 'test_plugin_install' );

and a multisite setup with

  • mysite.intern as main site
  • one.mysite.intern, two.mysite.intern, etc. as blog sites

Now, if you first activate the plugin individually on mysite.intern, and then select "Network Activate", the following is logged:

PHP Fatal error:  Please activate individually. in test_plugin.php on line 17
PHP Fatal error:  Cannot redeclare test_plugin_install() (previously declared in test_plugin.php:15) in test_plugin.php on line 15
PHP Fatal error:  Cannot redeclare test_plugin_install() (previously declared in test_plugin.php:15) in test_plugin.php on line 15

and only the last error is displayed on the admin page.

If the plugin is first activated on another blog site or isn't activated individually anywhere, only the expected "Please activate individually" error is thrown and displayed on network activation, as expected.

Wrapping the function test_plugin_install in a conditional only results in the next function in the plugin file triggering the redeclaration error.

Change History (4)

#1 @SergeyBiryukov
4 years ago

  • Keywords needs-patch added

Hi @ccprog, welcome to Trac. Thanks for the report, I have reproduced the issue as well.

#3 follow-up: @jeremyfelt
4 years ago

  • Milestone changed from Awaiting Review to Future Release

Would it make sense to automatically de-activate the plugin on the main site before activating it network wide or show a notice that the plugin should be deactivated first... or a mix?

#4 in reply to: ↑ 3 @mensmaximus
4 years ago

Replying to jeremyfelt:

Would it make sense to automatically de-activate the plugin on the main site before activating it network wide or show a notice that the plugin should be deactivated first... or a mix?

Wouldn't this be dangerous in case a plugin is not multisite compatible or if a plugin author explicitly decides to change the logic of a plugin in case it gets network activated?

What if options get stored in

$wpdb->base_prefix . "options"

after network activation instead of

$wpdb->prefix . "options"

opposed to a per site activation?

If a plugin was active before this could lead to loss of all stored options. We all know that from many child themes that do not share the settings with the parent. And there it is still the same table.

My first idea would be to disable the network activation by default as soon as a plugin has been installed on a per site basis. If there would be a filter for the network activation a plugin developer could decide whether to disable this default to allow network activation even if the plugin has already been activated on individual sites because his plugin stores options always at the same place.

In such a case deactivating wont hurt. The question is, is this something wordpress core has to solve or is it something a plugin developer should handle during activation despite the fact there is no way to deactivate network activation at the moment? (network: false would be helpful in the plugin file header as well as a filter for the network activation link in net-admin)?

Last edited 4 years ago by mensmaximus (previous) (diff)
Note: See TracTickets for help on using tickets.