Home Forums ArileWP Pro How to.. Automatically fill out ‘Alt Text, Title, Caption and Description’

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #16668
    Hans Goutier
    Participant

    Hello Keymaster,

    On https://wpkraken.io/blog/wordpress-images/ I read an article about it.
    Are you familiar with this – maybe you do have a smarter solution ๐Ÿ˜‰ – and if so, where exactly do I add this HTML code 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(
    // Specify the image (ID) to be updated
    ‘ID’ => $post_ID,
    // Set image Title to sanitized title
    ‘post_title’ => $my_image_title,
    // Set image Caption (Excerpt) to sanitized title

    ‘post_excerpt’ => $my_image_title,
    // Set image Description (Content) to sanitized title
    ‘post_content’ => $my_image_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 );
    }
    }

    #16674
    themearile
    Keymaster

    Hi Hans Goutier.

    Please did not add the above code because this code has some errors. So please add the below code for your work.

    /* 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(
    // Specify the image (ID) to be updated
    'ID' => $post_ID,
    // Set image Title to sanitized title
    'post_title' => $my_image_title,
    // Set image Caption (Excerpt) to sanitized title
    
    'post_excerpt' => $my_image_title,
    // Set image Description (Content) to sanitized title
    'post_content' => $my_image_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 );
    }
    }

    Let us know if it works.

    Thanks

    #16725
    Hans Goutier
    Participant

    Hello Keymaster,

    Where exactly do I add this HTML code in functions.php

    #16734
    themearile
    Keymaster

    Hi Hans

    Please add this code at the end of the code in the functions.php file.

    Thanks

    #16738
    Hans Goutier
    Participant

    Hello Keymaster,

    I followed your advice to add the code at the end of the functions.php file (Child theme, see below):

    <?php
    // Exit if accessed directly
    if ( !defined( ‘ABSPATH’ ) ) exit;

    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED – Do not modify or remove comment markers above or below:

    if ( !function_exists( ‘chld_thm_cfg_locale_css’ ) ):
    function chld_thm_cfg_locale_css( $uri ){
    if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) )
    $uri = get_template_directory_uri() . ‘/rtl.css’;
    return $uri;
    }
    endif;
    add_filter( ‘locale_stylesheet_uri’, ‘chld_thm_cfg_locale_css’ );

    if ( !function_exists( ‘chld_thm_cfg_parent_css’ ) ):
    function chld_thm_cfg_parent_css() {
    wp_enqueue_style( ‘chld_thm_cfg_parent’, trailingslashit( get_template_directory_uri() ) . ‘style.css’, array( ‘bootstrap’,’font-awesome’ ) );
    }
    endif;
    add_action( ‘wp_enqueue_scripts’, ‘chld_thm_cfg_parent_css’, 10 );

    // END ENQUEUE PARENT ACTION

    /* 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(
    // Specify the image (ID) to be updated
    ‘ID’ => $post_ID,
    // Set image Title to sanitized title
    ‘post_title’ => $my_image_title,
    // Set image Caption (Excerpt) to sanitized title

    ‘post_excerpt’ => $my_image_title,
    // Set image Description (Content) to sanitized title
    ‘post_content’ => $my_image_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 );
    }
    }

    Although I get the message that I am doing something wrong.

    #16847
    themearile
    Keymaster

    Hi Hans

    If you want to use the child theme of the ArileWP Pro theme to add the above code then we have already provided the ArileWP Pro Child Theme.

    So please download the ArileWP Pro Child Theme from your themearile account and add the above code in the child theme functions.php file.

    Let us know if it works.

    Thanks

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