Skip Navigation

[Resolved] Add Default Fallback image to custom image field

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by manishB-2 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1782719

Hi Team
I had created a custom field "product-image" to store my product image but I wanted to add a fallback image in case there is no value added to product-image field by user then a default image url should be automatically added as fallback image. For that I used a function given below But I get an error "Cannot use $this as parameter"

So can you please help me to make it work?

add_filter('wpcf_fields_slug_product-image_value_get', 'default_value_func', 10, 2);
function default_value_func($meta, $this){
if(empty($meta)){
$meta = 'hidden link';
}
return $meta;
}

#1783353

Hello, basically the error is telling you the variable name $this is restricted, and you can fix the error by replacing the variable name with something else like $this_context. In your filter's callback function code, the 2nd parameter is never used anyway so you only need to change it once in the default_value_func definition:

add_filter('wpcf_fields_slug_product-image_value_get', 'default_value_func', 10, 2);
function default_value_func($meta, $this_context){
if(empty($meta)){
$meta = '<em><u>hidden link</u></em>';
}
return $meta;
}

Please try this and let me know if it does not fully resolve the problem.

#1783585
broken-images.PNG

Hi
Thanks for your reply, I tested your code but it doesn't work and I am still getting broken images for all those products where images were not added in my custom field. So it means the fallback image is not being added with this function.

Can you please inspect your code and help me?

#1784389

My issue is resolved now. Thank you!