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;
}
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.
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?
My issue is resolved now. Thank you!