WordPress :: Divi Builder :: Gallery Module :: Use Image's Actual ALT Text
<?php
/* DON'T copy the first line (above) if your functions.php already has it.
* ---------------------------------------------------------------------- */
function my_et_theme_setup() {
if ( class_exists( 'ET_Builder_Module_Gallery' ) ) {
get_template_part( 'my-main-modules' );
$et_pb_gallery = new My_ET_Builder_Module_Gallery();
remove_shortcode('et_pb_gallery');
add_shortcode('et_pb_gallery', array( $et_pb_gallery, '_shortcode_callback' ) );
}
}
add_action( 'wp', 'my_et_theme_setup', 99 );
<?php
class My_ET_Builder_Module_Gallery extends ET_Builder_Module_Gallery {
function shortcode_callback( $atts, $content = null, $function_name ) {
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$gallery_ids = $this->shortcode_atts['gallery_ids'];
$fullwidth = $this->shortcode_atts['fullwidth'];
$show_title_and_caption = $this->shortcode_atts['show_title_and_caption'];
$background_layout = $this->shortcode_atts['background_layout'];
$posts_number = $this->shortcode_atts['posts_number'];
$show_pagination = $this->shortcode_atts['show_pagination'];
$gallery_orderby = $this->shortcode_atts['gallery_orderby'];
$zoom_icon_color = $this->shortcode_atts['zoom_icon_color'];
$hover_overlay_color = $this->shortcode_atts['hover_overlay_color'];
$hover_icon = $this->shortcode_atts['hover_icon'];
$auto = $this->shortcode_atts['auto'];
$auto_speed = $this->shortcode_atts['auto_speed'];
$module_class = ET_Builder_Element::add_module_order_class( $module_class, $function_name );
if ( '' !== $zoom_icon_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay:before',
'declaration' => sprintf(
'color: %1$s !important;',
esc_html( $zoom_icon_color )
),
) );
}
if ( '' !== $hover_overlay_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay',
'declaration' => sprintf(
'background-color: %1$s;
border-color: %1$s;',
esc_html( $hover_overlay_color )
),
) );
}
$attachments = array();
if ( ! empty( $gallery_ids ) ) {
$attachments_args = array(
'include' => $gallery_ids,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'post__in',
);
if ( 'rand' === $gallery_orderby ) {
$attachments_args['orderby'] = 'rand';
}
$_attachments = get_posts( $attachments_args );
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if ( empty($attachments) )
return '';
wp_enqueue_script( 'hashchange' );
$fullwidth_class = 'on' === $fullwidth ? ' et_pb_slider et_pb_gallery_fullwidth' : ' et_pb_gallery_grid';
$background_class = " et_pb_bg_layout_{$background_layout}";
$module_class .= 'on' === $auto && 'on' === $fullwidth ? ' et_slider_auto et_slider_speed_' . esc_attr( $auto_speed ) : '';
$output = sprintf(
'<div%1$s class="et_pb_module et_pb_gallery%2$s%3$s%4$s clearfix">
<div class="et_pb_gallery_items et_post_gallery" data-per_page="%5$d">',
( '' !== $module_id ? sprintf( ' id="%1$s"', esc_attr( $module_id ) ) : '' ),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( ltrim( $module_class ) ) ) : '' ),
esc_attr( $fullwidth_class ),
esc_attr( $background_class ),
esc_attr( $posts_number )
);
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$width = 'on' === $fullwidth ? 1080 : 400;
$width = (int) apply_filters( 'et_pb_gallery_image_width', $width );
$height = 'on' === $fullwidth ? 9999 : 284;
$height = (int) apply_filters( 'et_pb_gallery_image_height', $height );
list($full_src, $full_width, $full_height) = wp_get_attachment_image_src( $id, 'full' );
list($thumb_src, $thumb_width, $thumb_height) = wp_get_attachment_image_src( $id, array( $width, $height ) );
$data_icon = '' !== $hover_icon
? sprintf(
' data-icon="%1$s"',
esc_attr( et_pb_process_font_icon( $hover_icon ) )
)
: '';
$image_output = sprintf(
'<a href="%1$s" title="%2$s">
<img src="%3$s" alt="%6$s" />
<span class="et_overlay%4$s"%5$s></span>
</a>',
esc_url( $full_src ),
esc_attr( $attachment->post_title ),
esc_url( $thumb_src ),
( '' !== $hover_icon ? ' et_pb_inline_icon' : '' ),
$data_icon,
esc_attr( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) )
);
$orientation = ( $thumb_height > $thumb_width ) ? 'portrait' : 'landscape';
$output .= sprintf(
'<div class="et_pb_gallery_item%2$s%1$s">',
esc_attr( $background_class ),
( 'on' !== $fullwidth ? ' et_pb_grid_item' : '' )
);
$output .= "
<div class='et_pb_gallery_image {$orientation}'>
$image_output
</div>";
if ( 'on' !== $fullwidth && 'on' === $show_title_and_caption ) {
if ( trim($attachment->post_title) ) {
$output .= "
<h3 class='et_pb_gallery_title'>
" . wptexturize($attachment->post_title) . "
</h3>";
}
if ( trim($attachment->post_excerpt) ) {
$output .= "
<p class='et_pb_gallery_caption'>
" . wptexturize($attachment->post_excerpt) . "
</p>";
}
}
$output .= "</div>";
}
$output .= "</div><!-- .et_pb_gallery_items -->";
if ( 'on' !== $fullwidth && 'on' === $show_pagination ) {
$output .= "<div class='et_pb_gallery_pagination'></div>";
}
$output .= "</div><!-- .et_pb_gallery -->";
return $output;
}
}