ID, 'morenews-meta-image-options', true); $morenews_post_class = !empty($morenews_post_image_alignment) ? $morenews_post_image_alignment : $morenews_theme_class; if ($morenews_post_class != 'no-image') : $single_featured_image_view = morenews_get_option('single_featured_image_view'); ?>
ID); ?> post_excerpt))) { ?>

post_excerpt); ?>

/i', $morenews_post_content, $matches); $morenews_post_content = substr(get_post_field('post_content', $morenews_post_id), 0, 3000); $output = preg_match_all('//i', $morenews_post_content, $matches); if (isset($matches[1][0])) { $morenews_img_id = morenews_find_post_id_from_path($matches[1][0]); $morenews_img_url = wp_get_attachment_image_src($morenews_img_id, $morenews_thumbnail_size); if (isset($morenews_img_url[0])) { if ($return) { return wp_get_attachment_image($morenews_img_id, $morenews_thumbnail_size); } else { echo wp_kses_post(wp_get_attachment_image($morenews_img_id, $morenews_thumbnail_size)); } } else { // Check if external image URL is valid and display it if (@getimagesize($matches[1][0])) { if ($return) { ob_start(); ?> <?php echo esc_attr(basename($matches[1][0])); ?> <?php echo esc_attr(basename($matches[1][0])); ?> post_title)) { $attributes['alt'] = esc_attr($attachment->post_title); } else { // Fallback to post title if in a singular post context $post_id = get_post(); if ($post_id) { $attributes['alt'] = esc_attr(get_the_title($post_id->ID)); } else { // Fallback to image filename if no other options available $attributes['alt'] = esc_attr(morenews_get_image_alt_from_filename($attachment->guid)); } } } // Set 'loading' attribute for better performance $morenews_image_loading = morenews_get_option('global_toggle_image_lazy_load_setting'); if ($morenews_image_loading == 'enable') { $attributes['loading'] = 'lazy'; } // Set 'decoding' attribute to enhance rendering speed $morenews_image_decoding = morenews_get_option('global_decoding_image_async_setting'); if ($morenews_image_decoding == 'enable') { $attributes['decoding'] = 'async'; } return $attributes; } add_filter('wp_get_attachment_image_attributes', 'morenews_alt_text_optimized', 10, 3); /** * Extract alt text from an image filename as a fallback. * * @param string $image_url The URL of the image. * @return string The sanitized filename without extension. */ function morenews_get_image_alt_from_filename($image_url) { // Get the filename from the image URL and sanitize it for alt text usage. return esc_attr(pathinfo($image_url, PATHINFO_FILENAME)); } function morenews_add_img_attributes($allowedtags) { if (isset($allowedtags['img'])) { // Add additional attributes that plugins or core updates may introduce $allowedtags['img']['decoding'] = true; $allowedtags['img']['srcset'] = true; $allowedtags['img']['sizes'] = true; $allowedtags['img']['loading'] = true; $allowedtags['img']['data-*'] = true; // Support data-* attributes $allowedtags['img']['aria-*'] = true; // Support aria-* attributes for accessibility $allowedtags['img']['role'] = true; $allowedtags['img']['longdesc'] = true; $allowedtags['img']['usemap'] = true; $allowedtags['img']['referrerpolicy'] = true; $allowedtags['img']['style'] = true; // In case some plugins add inline styles $allowedtags['img']['crossorigin'] = true; } return $allowedtags; } add_filter('wp_kses_allowed_html', 'morenews_add_img_attributes');