Installing theme’s required plugin show critical error in Multisite

I’m having issues with a theme that needs a plugin when I install it I get a fatal error
There has been a critical error on your website. Please check your site admin email inbox for instructions.

I see this in Error Log
[14-May-2020 07:46:05 UTC] PHP Fatal error: Uncaught Error: Call to a member function add_cap() on null in /var/web/site/public_html/wp-content/plugins/cariera-plugin-off/inc/extensions/dashboard/my-account.php:390

  • Alessandro
    • Nightcrawler & Daydreamer

    Hello Antonio

    Yeap, it’s possible to trigger an issue on multisites if it’s not designed properly.

    I managed to remove action that trigger’s that issue. I created a code snippet wp-content/mu-plugins/wpmudev-cariera-multisite.php with the following code:// Uncaught Error: Call to a member function add_cap() on null
    add_action( 'setup_theme', function(){
    if( is_multisite() ){
    remove_action( 'init', 'cariera_user_capabilities' );
    }
    });

    This is a temporary fix until your plugin vendor releases a fix for this. Please notify plugin developer mentioning your issue. If a fix is released, please do remove this files as it removes a function that may be important across the plugin.

    Let us know if this worked for you. :nerd:

    Kind regards,
    Alessandro.

  • WebGas
    • Site Builder, Child of Zeus

    Thanks a lot, Alessandro, that’s very helpful!

    Can you give me some guidance on the info that I should provide to the developer to fix this issue? Is there maybe a reference on the correct way to code this for WPMU?

  • WebGas
    • Site Builder, Child of Zeus

    Hey Alessandro,

    so the dev is pushing back and is saying it’s a server misconfig.

    Are there some references that I can show him to prove that the function is not properly coded in the right way and possibly help him to fix the issue?

    Maybe some resources that explain the right way to use that function on WP Network installations?

  • Alessandro
    • Nightcrawler & Daydreamer

    Hey Antonio

    I test on my local development environment (Valet with PHP 7.4) on two separate WordPress installations, single and multisite, and I was able to replicate only on the multisite installation.

    I strongly believe that is not on us or our hosting. Your vendor should commit a test in a multisite and replicate it.

    As your vendor handles user roles and capabilities, it’s easy to mishandle something. We can not provide support for third party plugins but at least we can provide a workaround. :wink:

    The following action trigger the fatal error:add_action( 'init', 'cariera_user_capabilities' );

    Cariera plugin uses current_user_can() to check roles (candidate & employer) and add capabilities. Passing a role name to current_user_can() is no longer guaranteed to work correctly (see #22624). Instead, you may wish to check user role like this:add_action( 'setup_theme', function(){
    if( is_multisite() ){
    remove_action( 'init', 'cariera_user_capabilities' );
    $user = wp_get_current_user();
    if ( in_array( 'candidate', (array) $user->roles ) || in_array( 'employer', (array) $user->roles ) ) {
    add_action( 'init', 'cariera_user_capabilities' );
    }
    }
    });

    With that code we unhook check with current_user_can() and we hook it again to WordPress getting current user and checking its role. :nerd:

    I have updated the code on your website. The above should be consider as a permanent fix but you use it on your own responsibility.

    If you experience any issue, simply delete ‘wpmudev-cariera-multisite.php’ file from the ‘wp-content/mu-plugins’ directory.

    Kind regards,
    Alessandro.