Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1473837

    Hi Guys,

    This is my first enquiry so there might be an existing thread that I could not find.

    I’m building a new website to replace 4 older websites with the guidance of two people working in the industry. I myself am a bit handy and a quick learner, but not a professional at all. The website is almost done and the past three days I have spent putting Alternative Text to all the images in the Media Library. So I went from the dashboard to media library in the main menu. Opened the image, filled in the box where is says “alternative text”, clicked next (to go to the next image) and so on. When I was done with the entire library, I checked a couple of random pictures to see if all was there and to see if I was really, finally, done. It was. All the “.” and capitals were in the right place.

    Now two days later, I was just randomly in the media library to do something else and I noticed a picture without alt text. So I thought I missed one and filled it in. When I went to check on a couple others, I found out all the images were without alt text again. All the alt text is gone all of a sudden.

    I went back and put it back at a couple of random pictures to see if it will still be there later, so some have text again now. But all were empty just about an hour ago.

    Can someone please explain me what it is that happened or that I did wrong? Is there maybe a plugin that is intervering? It shouldn’t be intervering on the level of the media library right?

    What is the best way to put alt text to images? I’d like to know before I start all over again.

    Thanks in advance! I’m a bit nervous and obviously gutted about the time I spent. Hopefully my message comes across clear and neutral.

    • This topic was modified 1 month ago by Pietzaa.
    #1473862

    Hey pietervanzaanen,

    Thank you for the inquiry.

    Did you install any plugins or apply custom scripts after adding the alt text? It’s possible that a plugin or custom script caused the issue, as the theme does not include any function that would do this, nor would it make sense to create one. After adding the alt text again, consider creating a site backup or restore point. This will allow you to revert to a previous state if the issue occurs again.

    Best regards,
    Ismael

    #1473906

    What do you mean by:

    All the “.” and capitals were in the right place.

    What did you enter to the alt input field f.e.?

    #1473907

    Thanks Ismael for getting back. We are looking into what did what.

    I finished all the alt text before the weekend and in the weekend we were trying to finish up the website before launch. They people working on the website might have put some coding in and also tweaked some plugins. I probably have to re-enter all of the alt text by hand again for all of my 700+ photo’s then.

    Hi Guenni007,

    I entered at one picture: “Young surfers carrying surfboards under their arms are entering the water with an instructor.”
    Capitals: I read somewhere to make sure you start the Alt Text with a capital and also use them for names etc. Just as you would when officially writing.
    “.”‘s: I mean interpunction. So to make sure to use comma’s and dot’s (or full stops as you would call them in English.

    Cheers!

    #1473954

    Do all images got Titles or Descriptions too? – and do they differ from Alt Text?
    My question is that there might be a method to generate the alt text from one of the others.

    #1473973

    Hi,

    Thank you for the update.

    The people working on the website might have put some coding in and also

    Yes, it’s possible that they accidentally did something that affected the database and caused the alt text to be removed. Please redo this for a few images and wait for a day or two before proceeding with the rest of the images, just in case. Again, make sure to create a site backup after completing each task.

    And as mentioned by @Guenni007 above, you could programmatically copy the title or the description and apply them as alt text. Something like this in the functions.php file.

    WARNING: This will override the alt text of all images and replace them with the title.

    function av_update_existing_image_alt_texts() {
        $args = [
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_status'    => 'inherit',
            'posts_per_page' => -1,
        ];
    
        $images = get_posts($args);
    
        foreach ($images as $image) {
            $alt_text = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
            if (empty($alt_text)) {
                $title = $image->post_title;
                if (!empty($title)) {
                    update_post_meta($image->ID, '_wp_attachment_image_alt', $title);
                }
            }
        }
    }
    add_action('admin_init', 'av_update_existing_image_alt_texts');
    
    

    Best regards,
    Ismael

    #1474054

    Hi Ismael,

    Thanks for the reply. I redid all the titles and then run the script, the script however doesn’t overwrite existing alt text, only media in which the alt text box was empty. So now i remove all the wrong alt text and then run the script again. Cheers!

    #1474060

    @ismael – maybe your snippet works with the add_attachment hook too? So to say on uploading new images.

    Otherwise i found this snippet that creates title, alt and description by file name source:
    (bring the first Letter of each work to uppercase (ucwords) – and removes the hyphens etc )

    function my_set_image_meta_upon_image_upload( $post_ID ) {
    
    	// Check if uploaded file is an image, else do nothing
    
    	if ( wp_attachment_is_image( $post_ID ) ) {
    
    		$my_image_title = get_post( $post_ID )->post_title;
    
    		// Sanitize the title:  remove hyphens, underscores & extra spaces:
    		$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );
    
    		// Sanitize the title:  capitalize first letter of every word (other letters lower case):
    		$my_image_title = ucwords( strtolower( $my_image_title ) );
    
    		// Create an array with the image meta (Title, Caption, Description) to be updated
    		// Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
    		$my_image_meta = array(
    			'ID'	=> $post_ID,		// Specify the image (ID) to be updated
    			'post_title'	=> $my_image_title,		// Set image Title to sanitized title
    			'post_excerpt'	=> $my_image_title,		// Set image Caption (Excerpt) to sanitized title
    			'post_content'	=> $my_image_title,		// Set image Description (Content) to sanitized title
    		);
    
    		// Set the image Alt-Text
    		update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
    
    		// Set the image meta (e.g. Title, Excerpt, Content)
    		wp_update_post( $my_image_meta );
    
    	} 
    }
    add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.