# Warning: Invalid argument supplied for foreach() (Wordpress UMP Plugin)

**URL:** <https://www.phphelp.com/t/warning-invalid-argument-supplied-for-foreach-wordpress-ump-plugin/33517>\
**Category:** Help with WordPress\
**Created:** [May 17, 2021, 2:10pm UTC](https://www.phphelp.com/t/warning-invalid-argument-supplied-for-foreach-wordpress-ump-plugin/33517 "2021-05-17T14:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jryz](https://www.phphelp.com/user_avatar/www.phphelp.com/jryz/32/3705_2.png) [@jryz](https://www.phphelp.com/u/jryz)\
**Post date:** [May 17, 2021, 2:10pm UTC](https://www.phphelp.com/t/warning-invalid-argument-supplied-for-foreach-wordpress-ump-plugin/33517/1 "2021-05-17T14:10:20Z")

</div>

Can anyone tell me what went wrong in the code below to cause the Warning: Invalid argument supplied for foreach(). And how do I fix it? This is a script from the Ultimate Membership Pro plugin ‘shortcode.php’.

```auto
foreach ($data['custom_fields'] as $key => $value) {
        if (empty($data['custom_fields'][$key])){
            unset($data['custom_fields'][$key]);
        }
     }

     if (!empty($data['levels'])){

        $fullPath = IHC_PATH . 'public/views/membership_card.php';
        $searchFilename = 'membership_card.php';
        $template = apply_filters('ihc_filter_on_load_template', $fullPath, $searchFilename );
        wp_enqueue_script( 'ihc-print-this' );

        
        if (!empty($data['metas']['ihc_membership_card_custom_css'])){
            $output .=
            "<style>".
                stripslashes($data['metas']['ihc_membership_card_custom_css'])
            ."</style>";
        }
        $output .= ' <script>
                var printhisopt = {
                    importCSS: true,
                    importStyle: true,
                    loadCSS:"'.IHC_URL.'assets/css/style.css",
                    debug: false,
                    printContainer: true,
                    pageTitle: "",
                    removeInline: true,
                    printDelay: 333,
                    header: null,
                    formValues: false,
                };
                        jQuery(document).ready(function(){
                                jQuery(".fa-print-ihc").on("click", function(e){
                                        var idToPrint = jQuery( e.target ).attr( "data-id-to-print" );
                                        jQuery( "#" + idToPrint ).printThis(printhisopt);
                                });
                        });
            </script>';
        foreach ($data['levels'] as $lid => $level_data){
            if (in_array($lid, $exclude_levels)){
                continue;
            }
            ob_start();
            include $template;
            $output .= ob_get_contents();
            ob_end_clean();
        }
     }else{
        $output = '<div class="ihc-additional-message">'. __("No Membership Cards available based on your Subscriptions. SignUp on new Subscriptions or renew the existent one.", 'ihc').'</div>';
     }
 }
 return $output;

```

Here is the data structure for this `foreach` loop.

array(1) { [2]=\> array(12) { [“id”]=\> string(1) “3” [“user\_id”]=\> string(1) “4” [“level\_id”]=\> string(1) “2” [“start\_time”]=\> string(19) “2021-05-15 19:05:21” [“update\_time”]=\> string(19) “2021-06-15 19:05:27” [“expire\_time”]=\> string(19) “2021-06-15 19:05:21” [“notification”]=\> string(1) “0” [“status”]=\> string(1) “1” [“label”]=\> string(7) “Monthly” [“level\_slug”]=\> string(7) “monthly” [“badge\_image\_url”]=\> string(0) “” [“is\_expired”]=\> bool(false) } }

Source code file: [ufile.io/sux2awvl](https://ufile.io/sux2awvl)

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 19, 2021, 9:31am UTC](https://www.phphelp.com/t/warning-invalid-argument-supplied-for-foreach-wordpress-ump-plugin/33517/2 "2021-05-19T09:31:37Z")

</div>

Which FOREACH is throwing the error? You show two of them and you do not show us the error message.  
Most likely one has bad data being sent to it. The array data you showed us is an array of arrays. But, tell us which line is in error and the error message of it.

---

<div class="post-metadata">

**Author:** ![jryz](https://www.phphelp.com/user_avatar/www.phphelp.com/jryz/32/3705_2.png) [@jryz](https://www.phphelp.com/u/jryz)\
**Post date:** [May 26, 2021, 7:51am UTC](https://www.phphelp.com/t/warning-invalid-argument-supplied-for-foreach-wordpress-ump-plugin/33517/3 "2021-05-26T07:51:32Z")

</div>

I’ve found the solution.

```
if(isset($data['custom_fields']) && is_array($data['custom_fields'])){ //This line was missing previously
	 foreach ($data['custom_fields'] as $key => $value) {
	 	if (empty($data['custom_fields'][$key])){
			unset($data['custom_fields'][$key]);
		}
	 }
	}
	 if (!empty($data['levels'])){

		$fullPath = IHC_PATH . 'public/views/membership_card.php';
		$searchFilename = 'membership_card.php';
		$template = apply_filters('ihc_filter_on_load_template', $fullPath, $searchFilename );
		wp_enqueue_script( 'ihc-print-this' );

		
		if (!empty($data['metas']['ihc_membership_card_custom_css'])){
			$output .=
			"<style>".
				stripslashes($data['metas']['ihc_membership_card_custom_css'])
			."</style>";
		}
		$output .= '	<script>
				var printhisopt = {
					importCSS: true,
		            importStyle: true,
		            loadCSS:"'.IHC_URL.'assets/css/style.css",
		         	debug: false,
		        	printContainer: true,
		        	pageTitle: "",
		        	removeInline: true,
		        	printDelay: 333,
		        	header: null,
		        	formValues: false,
		        };
						jQuery(document).ready(function(){
								jQuery(".fa-print-ihc").on("click", function(e){
										var idToPrint = jQuery( e.target ).attr( "data-id-to-print" );
										jQuery( "#" + idToPrint ).printThis(printhisopt);
								});
						});
			</script>';
	 	foreach ($data['levels'] as $lid => $level_data){
	 		if (in_array($lid, $exclude_levels)){
	 			continue;
	 		}
	 		ob_start();
			include $template;
			$output .= ob_get_contents();
			ob_end_clean();
	 	}
	 }else{
		$output = '<div class="ihc-additional-message">'. __("No Membership Cards available based on your Subscriptions. SignUp on new Subscriptions or renew the existent one.", 'ihc').'</div>';
	 }
 }
 return $output;
```
