• Is there a plugin that can automatically alt tag images based on the file name of the image that you uploaded?

    For example:

    a-really-fat-cat.jpg

    This image’s alt tag would be:

    “a really fat cat”

    What I need is a plugin that removes the hyphens from the file name, but alt tags the image based on how the file is named. Is there something out there that does this? I can’t seem to find anything…

    Thanks.

Viewing 15 replies - 16 through 30 (of 32 total)
  • Timothy Jacobs

    (@timothyblynjacobs)

    function tj_add_alts( $content )
    {
        $dom = new DOMDocument();
        $dom->loadHTML($content);
        $dom->removeChild($dom->firstChild); // remove Doctype declaration
    
        foreach ($dom->getElementsByTagName( 'img' ) as $node) {
            //if ( trim( $node->getAttribute( 'alt' ) ) == "" ) { // Make sure alt tag has not been set
                $img = $node->getAttribute( 'src' );
                $file_name = pathinfo($img, PATHINFO_FILENAME); // get file name
                $name = preg_replace( '/[^A-Za-z0-9 ]/', ' ', $file_name); // remove special chars
                $name = preg_replace( '/\s{2,}/', ' ', $name); // remove extra white space
                $node->setAttribute( 'alt', $name );
            //}
        }
    
        $content = $dom->saveHTML();
    
        $content = str_replace( array( '<html>', '</html>', '<body>', '</body>' ), "", $content ); // remove auto added tags
    
        return $content;
    }
    add_filter('the_content', 'tj_add_alts', 999);

    this almost works, the problem is wp seems to have another filter somewhere that is overwriting the added attributes

    Thread Starter BidBoxUSA

    (@bidboxusa)

    where you commented “remove extra white space” is that still going to allow for spaces where the hyphens were?

    Timothy Jacobs

    (@timothyblynjacobs)

    The first one creates spaces where the hyphens were, or any other non alphanumeric characters. The second one removes whitespaces when there are more than one in a row. so a-fat--cat becomes a fat cat not a fat cat.

    Thread Starter BidBoxUSA

    (@bidboxusa)

    Alright, but it doesn’t work quite yet?

    Timothy Jacobs

    (@timothyblynjacobs)

    For some reason wp is overwriting the changes. Try it on your site, just drop it in your functions.php file

    Thread Starter BidBoxUSA

    (@bidboxusa)

    it’s for sure not going to change the images already uploaded?

    Timothy Jacobs

    (@timothyblynjacobs)

    It doesn’t change anything permanently. When wp renders a page it calls the 'the_content' filter. This hooks on to that filter, and does stuff, leaving the database and your posts unaffected. So if you want to remove it you can just comment out
    add_filter('the_content', 'tj_add_alts', 999);.

    Thread Starter BidBoxUSA

    (@bidboxusa)

    It actually breaks my sidebar.

    Timothy Jacobs

    (@timothyblynjacobs)

    Try this

    function tj_add_alts( $content )
    {
        if (is_single()) {
            $dom = new DOMDocument();
            $dom->loadHTML($content);
            $dom->removeChild($dom->firstChild); // remove doctype declaration
    
            foreach ($dom->getElementsByTagName( 'img' ) as $node) {
                //if ( trim( $node->getAttribute( 'alt' ) ) == "" ) { // Make sure alt tag has not been set
                    $img = $node->getAttribute( 'src' );
                    $file_name = pathinfo($img, PATHINFO_FILENAME); // get file name
                    $name = preg_replace( '/[^A-Za-z0-9 ]/', ' ', $file_name); // remove special chars
                    $name = preg_replace( '/\s{2,}/', ' ', $name); // remove extra white space
                    $node->setAttribute( 'alt', $name );
                    $node->setAttribute( 'title', $name );
                //}
            }
    
            $content = $dom->saveHTML();
    
            $content = str_replace( array( '<html>', '</html>', '<body>', '</body>' ), "", $content ); // remove auto added tags
        }
    
        return $content;
    }
    add_filter('the_content', 'tj_add_alts', 999);

    Thread Starter BidBoxUSA

    (@bidboxusa)

    That doesn’t break my sidebar, but it has no affect on the alt tags. It still outputs them the same as without that in my functions.php

    Timothy Jacobs

    (@timothyblynjacobs)

    yep. working on wondering why the changes don’t hold. Because if I test the output, the output is correct.

    Thread Starter BidBoxUSA

    (@bidboxusa)

    Ok BIG problem. Now when I preview posts it puts a TON of crazy characters in the body of the text.

    Like so:

    “Because of circumstances beyond my control (my office being under construction for +7 years), Ia€?ve got a ton of figures that I havena€?t opened yet. But, now that Ia€?ve got a semi-functioning space, Ia€?ve been going through container after container of figures that are dying to be opened.”

    I don’t want to publish it and see if it stays like that.

    Timothy Jacobs

    (@timothyblynjacobs)

    yeah comment it out, ill take a look.

    I would like to do this as well. I’d be fine with must removing the hyphens and setting the alt attribute to that.

    I could have sworn WordPress used to do this. Am I crazy? I’ve tried several SEO image plugins, and none of them seem to replace hyphens with spaces.

    If anybody has any ideas, I’d love to hear them. Thanks.

    I think there’s something going on in the core wordpress code that conflicts with the hyphen-to-space replacement. Easiest way for me is to use spaces in the image name prior to uploading. WordPress replaces the spaces with hyphens for the file name but leaves them in place for the alt tag.

Viewing 15 replies - 16 through 30 (of 32 total)
  • The topic ‘Automatically Alt Tag Images’ is closed to new replies.