TiTLE- und ALT-Text in Bildern fehlt

Hallo,

ich lade alle Bilder in der Mediathek hoch. Hier werden automatisch Title-tag und alt-tag ergänzt. Dies steuere ich über einen Code in der functions.php des child themes.

Beide Tags werden aber nicht im Quellcode ausgegeben!

Beispielseite: https://www.found-print.de/schluesselanhaenger-bedrucken-lassen/

166 Bilder sind geladen. 109 ohne ALT, 159 ohne TITLE.

Wo ist der Fehler?



/* Automatically set the image Title, Alt-Text, Caption & Description upon upload

--------------------------------------------------------------------------------------*/

add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );

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 );


}


Ich habe in einem anderen Forum einen Code für das Theme Divi gefunden, der diese Tags ausgeben soll.

Kann ich den verwenden?


// ALT-Tags aus der Mediathek auslesen

// ========================================================================== //

function get_image_meta( $image, $type = 'alt' ) {

  if ( ! $image ) {

    return '';

  }

 

  $output = '';

 

  if ( '/' === $image[0] ) {

    $post_id = attachment_url_to_postid( home_url() . $image );

  } else {

    $post_id = attachment_url_to_postid( $image );

  }

 

  if ( $post_id && 'title' === $type ) {

    $output = get_post( $post_id )->post_title;

  }

 

  if ( $post_id && 'alt' === $type ) {

    $output = get_post_meta( $post_id, '_wp_attachment_image_alt', true );

  }

 

  return $output;

}

/* Aktualisiert image alt text in Modulen */

function update_module_alt_text( $attrs, $unprocessed_attrs, $slug ) {

  if ( ( $slug === 'et_pb_image' || $slug === 'et_pb_fullwidth_image' ) && '' === $attrs['alt'] ) {

    $attrs['alt'] = get_image_meta( $attrs['src'] );

    $attrs['title_text'] = get_image_meta( $attrs['src'], 'title' );

  } elseif ( $slug === 'et_pb_blurb' && 'off' === $attrs['use_icon'] && '' === $attrs['alt'] ) {

    $attrs['alt'] = get_image_meta( $attrs['image'] );

  } elseif ( $slug === 'et_pb_slide' && '' !== $attrs['image'] && '' === $attrs['image_alt'] ) {

    $attrs['image_alt'] = get_image_meta( $attrs['image'] );

  } elseif ( $slug === 'et_pb_fullwidth_header' ) {

    if ( '' !== $attrs['logo_image_url'] && '' === $attrs['logo_alt_text'] ) {

      $attrs['logo_alt_text'] = get_image_meta( $attrs['logo_image_url'] );

    }

    

    if ( '' !== $attrs['header_image_url'] && '' === $attrs['image_alt_text'] ) {

      $attrs['image_alt_text'] = get_image_meta( $attrs['header_image_url'] );

    }

  }

 

  return $attrs;

}

/* Filter injection */

add_filter( 'et_pb_module_shortcode_attributes', 'update_module_alt_text', 20, 3 );

Comments

  • Hello,

    Sorry, but we provide this forum in English only.

    Can you rewrite your message in English, please?


    Thanks

  • Hi,

    I upload all the pictures in the media library. The title tag and alt tag are automatically added here. I control this via a code in the functions.php of the child theme. However, both tags are not output in the source code!

    Sample page: https://www.found-print.de/schluesselanhaenger-bedrucken-lassen/

    166 images are loaded. 109 without ALT, 159 without TITLE.

    Where is the mistake?


    The script in functions.php:

    /* Automatically set the image Title, Alt-Text, Caption & Description upon upload

    --------------------------------------------------------------------------------------*/

    add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );

    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 );


    }

    _______________________________________________________________________________

    I found a code for the Divi theme in another forum that is supposed to output these tags. Can i use it?


    // Read ALT tags from the media library

    // ========================================================================== //

    function get_image_meta( $image, $type = 'alt' ) {

      if ( ! $image ) {

        return '';

      }

     

      $output = '';

     

      if ( '/' === $image[0] ) {

        $post_id = attachment_url_to_postid( home_url() . $image );

      } else {

        $post_id = attachment_url_to_postid( $image );

      }

     

      if ( $post_id && 'title' === $type ) {

        $output = get_post( $post_id )->post_title;

      }

     

      if ( $post_id && 'alt' === $type ) {

        $output = get_post_meta( $post_id, '_wp_attachment_image_alt', true );

      }

     

      return $output;

    }

    /* Updates image alt text in modules*/

    function update_module_alt_text( $attrs, $unprocessed_attrs, $slug ) {

      if ( ( $slug === 'et_pb_image' || $slug === 'et_pb_fullwidth_image' ) && '' === $attrs['alt'] ) {

        $attrs['alt'] = get_image_meta( $attrs['src'] );

        $attrs['title_text'] = get_image_meta( $attrs['src'], 'title' );

      } elseif ( $slug === 'et_pb_blurb' && 'off' === $attrs['use_icon'] && '' === $attrs['alt'] ) {

        $attrs['alt'] = get_image_meta( $attrs['image'] );

      } elseif ( $slug === 'et_pb_slide' && '' !== $attrs['image'] && '' === $attrs['image_alt'] ) {

        $attrs['image_alt'] = get_image_meta( $attrs['image'] );

      } elseif ( $slug === 'et_pb_fullwidth_header' ) {

        if ( '' !== $attrs['logo_image_url'] && '' === $attrs['logo_alt_text'] ) {

          $attrs['logo_alt_text'] = get_image_meta( $attrs['logo_image_url'] );

        }

        

        if ( '' !== $attrs['header_image_url'] && '' === $attrs['image_alt_text'] ) {

          $attrs['image_alt_text'] = get_image_meta( $attrs['header_image_url'] );

        }

      }

     

      return $attrs;

    }

    /* Filter injection */

    add_filter( 'et_pb_module_shortcode_attributes', 'update_module_alt_text', 20, 3 );

  • Hi,

    What you ask for, requires file customization what in reference to Item Support Policy is not allowed. http://themeforest.net/page/item_support_policy

    So if you want to modify files and don't know how, you should contact your web developer. Item Policy says:

    Item support does not include services to modify or extend the item beyond the original features, style, and functionality described on the item page. For customization services that will help you tailor the item to your specific requirements, we recommend contacting the author to see if they privately offer paid customization services or checking out the great service providers on Envato Studio.


    If you need help with this script, please, check the following link:

    https://wpkraken.io/?ref=muffingroup

    There you will find people that will surely help you.


    Thanks

  • Hi,

    thanks for the fast respond.

    It is a pity that you cannot help me here why the TITLE-TAG and the ALT-TAG are not output in the source code.

Sign In or Register to comment.