Site logo

[Resolved] alt attribute on post-image

Home Forums Support [Resolved] alt attribute on post-image

Home Forums Support alt attribute on post-image

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1494006
    Anil

    Hi

    How to have alt attribute on post-image… alt is empty

    #1494212
    David
    Staff
    Customer Support

    Hi there,

    silly question – have you added an Alt tag to the image?

    #1494483
    Anil

    David, if there is no alt tag, can I pick post_title for alt….

    #1494701
    Tom
    Lead Developer
    Lead Developer

    You could try this:

    add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
        if ( empty( $atts['alt'] ) ) {
            $atts['alt'] = the_title_attribute( 'echo=0' ),
        }
    
        return $atts;
    } );

    Not sure if it will work or not, but worth a shot.

    #1495358
    Anil

    Thanks Tom, following code working, now it does not show empty alt””, it takes aticle’s title as alt attribute. It is helpful in a big and old website, where you do not have mentioned alt while uploading images … (this can be included in GP as well; rather showing empty alt””)

    Regards.

    add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
        if ( empty( $atts['alt'] ) ) {
            $atts['alt'] = the_title_attribute( 'echo=0' );
        }
        return $atts;
    } );
    #1495867
    Tom
    Lead Developer
    Lead Developer

    Glad it worked! 🙂

    #2384108
    James

    Hi! This code works great.

    However, when putting a site on PHP8 (from 7.4) it’s giving this error in the PHP error log:

    PHP Warning: Undefined variable $post_id in /home/username/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()’d code on line 2

    Is there a tweak that can be done to make it play nicely with PHP8?

    Thanks!

    #2384144
    David
    Staff
    Customer Support

    Hi there,

    your error points to Undefined variable $post_id – and that snippet isn’t using the $post_id variable.
    Do you have any other snippets ?

    #2384155
    James

    Ah, sorry David. Me being a twit on a Monday morning…

    The snippet I’ve got (to put the post title as featured image alts) is:

    add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
        if ( get_post_type( $post_id ) === 'post' ) {
            $atts['alt'] = the_title_attribute( 'echo=0' );
        }
        return $atts;
    } );

    I was sure I got it from here – but apparently not!

    #2384384
    David
    Staff
    Customer Support

    Oh no worries, my brain is super foggy today lol

    Try:

    
    add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
        if ( get_post_type( get_the_ID() ) === 'post' ) {
            $atts['alt'] = the_title_attribute( 'echo=0' );
        }
        return $atts;
    } );
    #2384399
    James

    Aha – brilliant!

    Thanks 🙂

    #2384421
    David
    Staff
    Customer Support

    You’re welcome

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.