CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   error after making changes to a wordpress plugin (http://www.codingforums.com/showthread.php?t=286132)

operapixie 01-20-2013 07:06 AM

error after making changes to a wordpress plugin
 
Hi there,
I'm using the recipress plugin for a client website that's nearly completed. I've posted yesterday on their forums as well, but haven't gotten any response and it looks like most folks aren't getting timely responses. So I thought I'd ask for some assistance over here.

Basically, the client asked to have the heading "Instructions" changed to "Prescription". I know I could have just changed the language file, but this client isn't very saavy and would have gotten generally confused in the admin area as well so I figured I'd change all instances of "instruction/instructions" and "Instruction/Instructions" in all files. For the most part, everything came out great. The script displays fine and everything. But I'm getting two errors and I don't know how to solve them (I can occasionally figure out how to write my own snippet, but I am no programmer).

Following are the error messages I'm getting:

Warning: Invalid argument supplied for foreach() in /path/wp-content/plugins/recipress/php/functions.php on line 240

Warning: Invalid argument supplied for foreach() in /path/wp-content/plugins/recipress/php/functions.php on line 288

And the two referenced lines of code are identical:

PHP Code:

foreach($prescriptions as $prescription) { 

http://dev.yourbusybee.com/kaleco2/new-mama/ is one such example of a page this error appears on.

Any help would by hugely appreciated.

Thanks in advance.

~Laura

sunfighter 01-20-2013 05:03 PM

foreach($prescriptions as $prescription) { is OK so it must be someplace else in the code.

operapixie 01-20-2013 07:19 PM

Ok. But that's what's on the rows it's saying the errors are in. How would I figure out where else the error would be coming from.

sunfighter 01-21-2013 04:12 PM

Post your code

operapixie 01-21-2013 05:10 PM

I'm sorry, I don't know what part of the code to post. There are a bunch of files associated with this plugin. Do you just want all the code from the referenced file?

sunfighter 01-21-2013 11:18 PM

First thing to check is right from the manual:
foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

What things should look like:
PHP Code:

<?php
$prescriptions 
= array('step one''step two''step three''step four');
reset($prescriptions);  // TO MAKE SURE THE ARRAY IS POINTING AT FIRST ELEMENT

foreach ($prescriptions as $prescription) {
    echo 
"Next Value: $prescription<br />\n";
}
?>

Is $prescriptions an array? Is it an object?

AndrewGSW 01-21-2013 11:28 PM

Sorry, but from the manual:

Quote:

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
reset() is only required if you have used next() or some other method to move the internal pointer.

Inigoesdr 01-22-2013 01:30 AM

Quote:

Originally Posted by operapixie (Post 1307632)
Ok. But that's what's on the rows it's saying the errors are in. How would I figure out where else the error would be coming from.

That error means the first argument, $prescriptions, that you are passing to foreach() loop is supposed to be an array, but it isn't. If you dump the $prescriptions variable before the foreach() loop you'll see what the actual value is:
PHP Code:

var_dump($prescriptions); 


operapixie 01-22-2013 10:01 PM

Since I really and truly have no clue what portion of the code I would need to post, I'm going to assume that you need to see the entire "function.php" file contents in order to help me here. The above comments, I'm sure, should make loads of sense if I really knew php at a slightly higher level, but alas I'm still a beginner with it. Hopefully someone will be able to see what the problem is by looking at this?

I should note that I finally HAVE an entry with "Prescriptions" in it, and they do not appear on the page:

Here

PHP Code:

<?php

// add to an array
function recipress_array_insert($arr1$key$arr2$before false) {
    
$index array_search($keyarray_keys($arr1));
    
    if(
$index === FALSE)
        
$index count($arr1);
    else
        if(!
$before)
            
$index++;

    
$end array_splice($arr1$index);
    return 
array_merge($arr1$arr2$end);
}

// remove from an array
function recipress_array_remove() { 
    
$args func_get_args(); 
    
$array $args[0]; 
    
$keys array_slice($args,1); 
     
    foreach(
$array as $k=>$v) { 
        if(
in_array($k$keys)) 
            unset(
$array[$k]); 
    } 
    return 
$array


// hasRecipe
function has_recipress_recipe() {
    global 
$post;
    
$hasRecipe false;
    
$meta get_post_meta($post->ID'hasRecipe'true);
    if(
$meta == 'Yes'$hasRecipe true;
    return 
$hasRecipe;
}

// post type
function recipress_post_type() {
    
$type recipress_options('post_type') ? recipress_options('post_type') : 'post';
    
    return 
$type;
}

// output
function recipress_output() {
    
// determine where to output
    
$output false;
    
$outputs recipress_options('output');
    if(!isset(
$outputs)) {
        if (
is_single()) {
            
$output true;
        }
    }
    else {
        if(
is_home() && in_array('home'$outputs)) $output true;
        if(
is_single() && in_array('single'$outputs)) $output true;
        if(
is_archive() && in_array('archive'$outputs)) $output true;
        if(
is_search() && in_array('search'$outputs)) $output true;
    }
    return 
$output;
}

// recipress_theme
function recipress_theme() {
    
$theme 'recipress-light';
    
$theme_settings recipress_options('theme');
    if(isset(
$theme_settings)) $theme $theme_settings;
    return 
$theme;
}

// recipress_gen_summary
function recipress_gen_summary() {
   
$excerpt get_the_content();
   
$excerpt strip_tags(trim($excerpt));
   
$new_excerpt '';
   
$charlength 140;
   if(
strlen($excerpt)>$charlength) {
       
$subex substr($excerpt,0,$charlength-5);
       
$exwords explode(" ",$subex);
       
$excut = -(strlen($exwords[count($exwords)-1]));
       if(
$excut<0) {
            
$new_excerpt .= substr($subex,0,$excut);
       } else {
               
$new_excerpt .= $subex;
       }
       
$new_excerpt .= "&hellip;";
   } else {
       
$new_excerpt .= $excerpt;
   }
   return 
$new_excerpt;
}

// recipress_add_photo
function recipress_add_photo() {
    
$add_photo false;
    if(!
current_theme_supports('post-thumbnails') || (current_theme_supports('post-thumbnails') && recipress_options('use_photo') == 'no'))
        
$add_photo true;
    return 
$add_photo;
}

// recipress_use_taxonomies
function recipress_use_taxonomies() {
    
$taxonomies = array('cuisine''course''skill_level');
    
$set_taxonomies recipress_options('taxonomies');
    if(
$set_taxonomies !=''$taxonomies $set_taxonomies;
    return 
$taxonomies;
}

// recipress_time
function recipress_time($minutes$attr null) {
    if (
$minutes != '') {
        
$time '';
        
$hours '';
        if(
$minutes 60) {
            
$hours floor($minutes 60);
            
$minutes $minutes floor($minutes/60) * 60;
        }
        if (
$attr == 'iso') {
            
$time $hours.':'.$minutes;
            
$time strtotime($time);
            if (
$hours != '' $time 'PT'.$hours.'H'.$minutes.'M';
            else 
$time 'PT'.$minutes.'M';
        } else {
            
$h __('hrs''recipress');
            
$m __('mins''recipress');
            if(
$hours 2$h __('hr''recipress');
            if(
$minutes 02$m __('min''recipress');
            if (
$hours != '' $time $hours.' '.$h.' '.$minutes.' '.$m;
            else 
$time $minutes.' '.$m;
        }     
    return 
$time;
    }
}

// function for outputting recipe items
// ----------------------------------------------------
function recipress_recipe($field$attr null) {
    global 
$post;
    
$meta get_post_custom($post->ID);
    
    switch(
$field) {
        
// title
        
case 'title':
            
$title get_the_title().' '.__('Recipe''recipress');
            
$recipe_title $meta['title'][0];
            if(
$recipe_title$title $recipe_title;
            return 
$title;
        break;
        
        
// photo
        
case 'photo':
            if(
current_theme_supports('post-thumbnails') && recipress_options('use_photo') != 'no'
                
$photo get_the_post_thumbnail($post->ID'thumbnail'$attr);
            else {
                
$photo_id $meta['photo'][0];
                
$photo wp_get_attachment_image($photo_id'thumbnail'false$attr);
            }
            return 
$photo;
        break;
        
        
// summary
        
case 'summary':
            return 
$meta['summary'][0];
        break;
            
        
        
// cuisine
        
case 'cuisine':
            
$cuisine get_the_term_list$post->ID'cuisine'$attr);
            return 
$cuisine;
        break;
        
        
// course
        
case 'course':
            
$course get_the_term_list$post->ID'course'$attr);
            return 
$course;
        break;
        
        
// skill_level
        
case 'skill_level':
            
$skill_level get_the_term_list$post->ID'skill_level'$attr);
            return 
$skill_level;
        break;
        
        
// prep_time
        
case 'prep_time':
            
$prep_time $meta['prep_time'][0];
            
$prep_time recipress_time($prep_time$attr);
            return 
$prep_time;
        break;
        
        
// cook_time
        
case 'cook_time':
            
$cook_time $meta['cook_time'][0];
            
$cook_time recipress_time($cook_time$attr);
            return 
$cook_time;
        break;
        
        
// ready_time
        
case 'ready_time':
            
$prep_time $meta['prep_time'][0];
            
$cook_time $meta['cook_time'][0];
            
$other_time $meta['other_time'][0];
            
$ready_time $prep_time $cook_time $other_time;
            
$ready_time recipress_time($ready_time$attr);
            return 
$ready_time;
        break;
        
        
// yield
        
case 'yield':
            
$yield $meta['yield'][0];
            
$servings $meta['servings'][0];
            if(
$yield && $servings$yield $yield.' ('.$servings.' '.__('Servings''recipress').')';
            if(!
$yield && $servings$yield $servings.' '.__('Servings''recipress');
            return 
$yield;
        break;
        
        
// cost
        
case 'cost':
            
$cost $meta['cost'][0];
            return 
$cost;
        break;
        
        
// ingredients
        
case 'ingredients':
            
$ingredients $meta['ingredient'];
            foreach(
$ingredients as $ingredient) {
                
$ingredients unserialize($ingredient);
            }    
            
$output $ingredients;
            
            return 
$output;
        break;
        
        
// prescriptions
        
case 'prescriptions':
            
$prescriptions $meta['prescription'];
            foreach(
$prescriptions as $prescription) {
                
$prescription unserialize($prescription);
            }
            
$output $prescriptions;
            
            return 
$output;
        break;
        
        default:
            return 
$meta[$field][0];
    } 
// end switch
    
}

// recipress_ingredients_list
function recipress_ingredients_list() {
    
$ingredients recipress_recipe('ingredients');
    
$output '<ul class="ingredients">';
    foreach(
$ingredients as $ingredient) {
        
$amount $ingredient['amount'];
        
$measurement $ingredient['measurement'];
        
$the_ingredient $ingredient['ingredient'];
        
$notes $ingredient['notes'];
        
        if(!
$ingredient['ingredient']) continue;
        
        
$output .= '<li class="ingredient">';
        if (isset(
$amount) || isset($measurement)) 
            
$output .= '<span class="amount">'.$amount.' '.$measurement.'</span> ';
        if (isset(
$the_ingredient))
            
$term get_term_by('name'$the_ingredient'ingredient');
            
$output .= '<span class="name">';
            if (!empty(
$term)) $output .= '<a href="'.get_term_link($term->slug'ingredient').'">';
            
$output .= $the_ingredient;
            if (!empty(
$term)) $output .= '</a>';
            
$output .= '</span> ';
        if (isset(
$notes)) 
            
$output .= '<i class="notes">'.$notes.'</i></li>';
    }
    
$output .= '</ul>';
    
    return 
$output;
}

// recipress_prescriptions_list
function recipress_prescriptions_list() {
    
$prescriptions recipress_recipe('prescriptions');
    
$output '<ol class="prescriptions">';
    foreach(
$prescriptions as $prescription) {
        
$size recipress_options('prescription_image_size');
        if (!isset(
$size)) $size 'large';
        
$image $prescription['image'] != '' wp_get_attachment_image($prescription['image'], $sizefalse, array('class' => 'align-'.$size)) : '';
        
        
$output .= '<li>';
        if (
$size == 'thumbnail' || $size == 'medium'
            
$output .= $image;
        
$output .= $prescription['description'];
        if (
$size == 'large' || $size == 'full'
            
$output .= '<br />'.$image;
        
$output .= '</li>';
    }
    
$output .= '</ol>';
    
    return 
$output;
}

// recipress_credit
function recipress_credit() {
    
$credit recipress_options('credit');
    if(isset(
$credit) && $credit == 1)
        return 
'<p class="recipress_credit"><a href="http://www.recipress.com" target="_target">WordPress Recipe Plugin</a> by ReciPress</p>';
}


?>


sunfighter 01-23-2013 02:32 PM

This is a guess. After line 240 you have:
PHP Code:

foreach($prescriptions as $prescription) {
                
$prescription unserialize($prescription);
            }
            
$output $prescriptions;

            return 
$output;
        break; 

Change the line
PHP Code:

$prescription unserialize($prescription); 

To
PHP Code:

$prescriptions unserialize($prescription); 

Adding an s.

operapixie 01-23-2013 09:26 PM

Thank you awfully much. I have somehow solved the issue, though I'm not really sure how. It seems that the recipress plugin doesn't save data to the database or something. When I changed "Instructions" to "Prescriptions", and even searched for the same in PHPMyAdmin, any I had entered were gone. When I re-entered them, the errors disappear. Surely it would be better to have an if/then statement so that if there ARE no "Prescriptions", nothing appears at all. Aw well. I'll work on that later I suppose.

Thank you all for taking a look at things.


All times are GMT +1. The time now is 07:17 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.