Skip to content

Instantly share code, notes, and snippets.

@aslamdoctor
Created January 25, 2022 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslamdoctor/58e851d1395ab55dfa77c06a551038a2 to your computer and use it in GitHub Desktop.
Save aslamdoctor/58e851d1395ab55dfa77c06a551038a2 to your computer and use it in GitHub Desktop.
Woocommerce : Change image alt text to product name
<?php
/**
* Change image alt text to product name
*/
add_filter('wp_get_attachment_image_attributes', 'wpmix_attachement_image_attributes', 20, 2);
function wpmix_attachement_image_attributes( $attr, $attachment ){
// Get post parent
$parent = get_post_field( 'post_parent', $attachment);
// Get post type to check if it's product
$type = get_post_field( 'post_type', $parent);
if( $type != 'product' ){
return $attr;
}
/// Get title
$title = get_post_field( 'post_title', $parent);
$attr['alt'] = esc_attr($title);
$attr['title'] = esc_attr($title);
return $attr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment