Skip to content
Advertisement

Warning: Invalid argument supplied for foreach() WordPress (UMP)

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’.

    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: shortcode.php

Advertisement

Answer

I’ve found the solution.

if(isset($data['custom_fields']) && is_array($data['custom_fields'])){ //This line of code is 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;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement