just change the $post->ID with get_post_thumbnail_id(), and possibly we can delete also the global $post;
if( ! ( function_exists( 'add_alt_image_content' ) ) ) {
function add_alt_image_content( $content ) {
if ( is_single() && in_the_loop() && is_main_query() ) {
global $post;
$image = get_post(get_post_thumbnail_id());
$image_title = $image->post_title;
$pattern = '~(<img.*? alt=")("[^>]*>)~i';
$replace="$1".$image_title.'$2';
$content = preg_replace( $pattern, $replace, $content );
return $content;
}
}
}
add_filter( 'the_content', 'add_alt_image_content' );
Related Posts:
- Remove title attribute from images
- Alter image output in content
- add data-attribute to all images inside the_content()
- How to add custom classes to figure element only if image has caption?
- How to filter the image embed code based on link type
- Add custom text in the media library image meta area
- How might one programmatically set the link for all images in all posts?
- Removing image dimensions from `the_content`
- Filter to remove image dimension attributes?
- Set default image link target in Gutenberg image block
- Where to hook into post content?
- Is there a hook which fires after all thumbnails are generated?
- Change WordPress image URLs via filter
- Always use for post images
- Change html structure of all img tags in WordPress
- Default Image Link Removal
- hook into completed image upload filter
- Saving the pre-sanitized filename of an attachment as the Title or Caption
- Need help building a filter to edit the output of “image_send_to_editor”
- Filter Media Library by author or post_parent
- Preview Image Path in Admin Section
- How to display a shortcode caption somewhere other than the_content
- How can I remove the image attachment ALT field?
- Filter to modify post_title after image upload?
- Wrap all post images inside a div element
- Change html structure of all img tags in WordPress
- why does the add_action(‘the_content’) overwrite my page
- Disable wordpress image sizes generation
- Extend core block attributes in post content
- What’s the proper way to use the get_image_tag filter?
- Editing
- Can I the caption shortcode to set caption to a data attribute, and with the image’s alignment intact?
- How to call function at the bottom of post using plugin?
- How to change the order (priority) of registered filters (or actions) (e.g. for the_content)?
- How to get image ID to use inside wp_read_image_metadata()?
- Add “data-” attribute to image links
- How to retrieve the postID in a “image_send_to_editor” hook function?
- Modify Image Source With The_Content Filter?
- Post Content, Special Characters and Filters
- Extracting gallery images in WordPress 3.5 on index.php
- Is there any action/filter hook to use when an image gets edited in the editor?
- How to create ‘Local’ filters for WordPress srcset sizes in template parts?
- How to change image atributes right before an image to be saved?
- Add content as soon starts
- Change align classes for images
- WP 5.3 Removing Default WordPress Image Sizes
- Generate images to be associated for tag and category?
- Omit image captions from get_the_content()
- Include WooCommerce 2’s prettyphoto scripts on all site pages
- Filter for post_thumbnail: link to small src and add data-attribute [duplicate]
- Remove Image Sizes in Media Settings Page
- How can I use wp_save_image_editor_file filter?
- How to stop WordPress from completely overriding my tags in my templates?
- Force default OG:Image – Yoast SEO [closed]
- Is it possible to enable the ‘Link To’ field under ‘Attachment Display Settings’ for a Featured Image?
- Insert Content Before div#main from the functions.php File
- the_content not showing responsive image attributes srcset or sizes
- Prevent WP from wrapping html in caption shortcode after ‘image_send_editor’ filter is applied
- How can I change the to for lazyload wp smilies
- Add hook after content without formatting
- How to display a warning when an uploaded image is too small?
- Scanning for custom embed and prefetching
- How to remove image size inline style in article and include caption
- How to change an image source when rendering on frontend?
- How to change Gallery image url in product page ? In function.php?
- How to generate alt attributes with php / filters?
- Wrap post images with a link, not figure (image_send_to_editor doesn’t work)
- How to trigger “wp_handle_upload_prefilter” filter when uploading an image programatically?
- Generate links on all images in posts – is there a hook?
- js alert in add_filter function for image_send_to_editor
- Why do images inserted in the post content via the_content() go off the max-width?
- Dinamically modifying attributes of images on posts
- How to attach post ID to image links in WordPress tinymce editor
- How to best adjust images to a max content width of 490px?
- Filter Content on all Post Types
- image_send_to_editor filter – Unwanted paragraphs and line breaks
- Function the_content
- Image filter works on attachement pages but not posts. I can’t get the image ID
- Surround uploaded image link with div
- Add class to all parent elements inside the_content
- Rename uploads filenames to POST-ID on upload
- Show uploaded images in full size
- How to access & display images from a post without textual content ( and vice versa) according to best wordpress practices
- Custom filename when pasting an image from clipboard
- I tried to move the featured image below the paragraphs but now it is displaying twice
- How to add a link to media file for every image inserted in content?
- Profile Image displaying on Wrong side of WordPress Navigation Menu
- WordPress speed slow after .5 million single image post via wp-cli
- Upload thousands of images to WordPress without plugin?
- Change permalink structure hidden button edit
- Image LazyLoad plugin not loading jQuery dependancies with wp_enqueue_script in WP 3.3.1
- Rewrite inline image markup
- wordpress makes duplicate images of different size
- Landing Page with HTML CSS JS and Images on wordpress
- Images missing from Blog post page
- Page size too big due to images
- Are WP images responsive by default?
- Display ajax preloader for large images within posts
- Why some images are not allowed to be edited only for thumbnails?
- WordPress Theme: replace logo image with title site if file doesn´t exist on server