setting up a Woocommerce shop, I wanted to add custom alt and title tags to product images, based on product title, tags and one attribute. I added code to child functions.php, and it seemed to work – both on front and in Media manager I got the alt and title tags I wanted, everything seemed ok.

Until I tried to edit a product page on the admin area….it loads only halfway (the whole section below the main content box is missing), shows no product tags added, and by the product image it says :

Fatal error: Call to a member function get_tags() on null in functions.php on line 112

My code in the child functions.php ($authortags beeing line 112):

// Change images alt and title tag 
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post, $product;
if ($post->post_type == 'product') {
    $title = $post->post_title;
    $authortags = strip_tags ($product->get_tags());
    $editor = $product->get_attribute( 'pa_szerkesztette' );

    $attr['alt'] = $title .' '. $authortags .' '. $editor;
    $attr['title'] = $title .' '. $authortags .' '. $editor;
}
return $attr;
}   

It’s the same with all product pages. I guess the problem is stripping the tags, and I should find another method to get them in the code, but nothing I’ve tried did work (my php knowledge is rather limited…) .

Could anyone help me, what am I doing wrong, how to modify the code to get this work?
Thanks

Read more here: Woocommerce: code for adding custom alt and title tag to product images works, but brakes product admin page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: