1. Home
  2. Knowledge Base
  3. Tasty Pins
  4. Customizing Tasty Pins
  5. Filter: Include alt text on a Tasty Pins hidden image

Filter: Include alt text on a Tasty Pins hidden image

If you're interested in adding alt text on hidden images that don't currently have alt text, you can use this filter to add it in.

Just add in the following snippet to your theme's functions.php file:

***Before doing this, make sure that you have access to your site's files via FTP (the editor in WordPress is not sufficient in case problems arise) and that you are comfortable with editing your functions.php file. Any minor error in the functions.php file can result in the “white screen of death,” which is easily fixed via FTP but is difficult to fix otherwise (and a nuisance as your site will be unavailable until fixed).***

/** 
* Includes alt text on a Tasty Pins hidden image if none yet exists. 
* 
* @param string $image_content Image content HTML string. 
* @param integer $hidden_image Hidden image ID. 
* @return string 
*/

add_filter( 
    'tasty_pins_hidden_image_html', 
    function( $image_content, $hidden_image ) { 
        $alt_text = get_post_meta( $hidden_image, '_wp_attachment_image_alt', true ); 
        if ( ! $alt_text || false !== stripos( $image_content, 'alt="' ) ) { 
            return $image_content; 
        } 
        return str_replace( '<img ', '<img alt="' . esc_attr( $alt_text ) . '" ', $image_content ); 
    }, 
    10, 
    2
);

 

Was this article helpful?

Related Articles

Need Support?

Can't find the answer you're looking for?
Contact Support