Add custom image sizes to media uploader

Invalid argument supplied for foreach means that the X in foreach X as ... is not an array. You can prevent this error by type casting; add (array) before the argument in the foreach statement. This will turn your variable into an array, essentially. In your code, the change would be…

foreach ( (array) $attach_meta['sizes'] as $size => $value ) {

Now keep in mind that this won’t fix the real issue here, which is that your code is expecting $attach_meta['sizes'] to be an array and it is not. You may want to refactor your code to handle that scenario.

Leave a Comment