TGMPA / TGM-Plugin-Activation

TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install, update and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference bundled plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.
http://tgmpluginactivation.com/
GNU General Public License v2.0
1.75k stars 431 forks source link

Invalid argument supplied for foreach() in... class-tgm-plugin-activation.php #753

Closed warever37 closed 5 years ago

warever37 commented 5 years ago

Hi,

I'm getting this error in the back-end of my Wordpress website, but it's not showing on the font-end which is good.

Why am I getting this error?

NOTE: I'm not a programmer and I have very little knowledge of coding, i.e. I didn't change any files, it happened suddenly.

The PHP file is up-to-date (version 2.6.1) and my Wordpress version and all the other plugins are up-to-date as well.

I have attached a screenshot of both the error and the code on the specified line.

What seems to be the problem?

Thank you. 1 error 2 php code

GaryJones commented 5 years ago

@warever37 Can you please provide the code where you define your plugins, and call tgmpa()?

warever37 commented 5 years ago

Hi @GaryJones, attached is the file.

class-tgm-plugin-activation.zip

GaryJones commented 5 years ago

@warever37 That's the TGMPA file, rather than the config file which is needed.

Please see and follow http://tgmpluginactivation.com/configuration/, which is needed for you to define which plugins you want to mark as required or recommended.

Without that, you will indeed get the error above, since $plugins in the foreach won't have been defined.

GaryJones commented 5 years ago

@jrfnl We could add some sort of validation for $plugins, before calling foreach, to help authors better identify what the issue is?

warever37 commented 5 years ago

@GaryJones Unfortunately I have no experience at all in this :(

I've read the page you were referring to, but couldn't understand what to do.

Is the config file located in a particular folder that I can send to you?

GaryJones commented 5 years ago

If you're seeing the error, but you're not a developer, then I'm guessing that a theme or plugin you've got active is the cause of the problem.

Try switching themes and see if the issue disappears. If so, contact the theme author and get them to fix it.

If it doesn't remove the warning, then try disabling plugins until you can narrow down which is the cause, and contact that author.

warever37 commented 5 years ago

@GaryJones I agree. I think the issue is with the theme.

I'll contact the theme developers and see if they can get this fixed.

Thanks.

jrfnl commented 5 years ago

@jrfnl We could add some sort of validation for $plugins, before calling foreach, to help authors better identify what the issue is?

The only way you can get this error is by calling tgmpa() without the plugins parameter - which would throw a notice about a missing required param - or with $plugins not being a Traversable or array. In that case, the theme/plugin has no business calling TGMPA anyway and the error alerts them to this. We could do a try/catch or an is_iterable() or is_array() check and throw a notice instead, but the net effect would be the same.

In this case, the Sydney theme doesn't initialize the $plugins variable - which would throw a notice the dev clearly didn't see or ignored -, conditionally defines the plugins to recommend, but still calls tgmpa() even when these plugins are installed and the $plugins variable therefore is still undefined. This is just sloppy coding in the theme by a theme author who clearly hasn't got error notices turned on during development. Just adding a $plugins = array(); at the start of their function would already fix this. Wrapping the call to tgmpa() within a check for if ( ! empty( $plugins ) ) also would. See: https://themes.trac.wordpress.org/browser/sydney/1.48/functions.php#L422-L441

I saw a similar question earlier this week on the fora: https://wordpress.org/support/topic/warning-invalid-argument-supplied-for-foreach-108/ so I suspect it's something in the latest release of the Sydney theme.

GaryJones commented 5 years ago

Pinging @athemes for their attention then.