警告:为 foreach() Wordpress (UMP) 提供的参数无效
Warning: Invalid argument supplied for foreach() Wordpress (UMP)
任何人都可以告诉我下面的代码出了什么问题导致警告:为 foreach() 提供的参数无效。我该如何解决?这是来自 Ultimate Membership Pro 插件的脚本 '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;
这是这个 foreach
循环的数据结构。
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) } }
源代码文件:shortcode.php
我找到了解决方案。
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;
任何人都可以告诉我下面的代码出了什么问题导致警告:为 foreach() 提供的参数无效。我该如何解决?这是来自 Ultimate Membership Pro 插件的脚本 '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;
这是这个 foreach
循环的数据结构。
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) } }
源代码文件:shortcode.php
我找到了解决方案。
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;