1

I 've found the following code to fill the ALT attribute everytime I upload and image in Wordpress. The code gets the name of the image to fill the attributes, but I want he gets the title post, how can modify it? I try to change get_post( $post_ID )->post_title to get_the_title($page->ID) but not work. Many thanks!

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

1 Answer 1

0

Well, changing get_post( $post_ID )->post_title to get_the_title($page->ID) won't work, because the variable $page isn't defined anywhere in your function...

If you'll take a look at add_attachment hook, then you'll see that it takes only one parameter:

$post_ID - (int) Attachment ID.

So you don't have the ID of post that it's attached to. And to be honest, you can't have such post, because the attachment can be uploaded directly in Media Library, so it won't be attached to any post...

But the add_attachment action is called when the post is already in DB, so you can get it's parent (and if attachment was uploaded to some post, then that post will be set as parent).

So something like this might work:

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 ) ) {
        $attachment = get_post( $post_ID );
        $my_image_title = $attachment->post_title;

        if ( $attachment->post_parent ) { // if it has parent, use it's title instead
            $my_image_title = get_post_field( 'post_title', $attachment->post_parent );
        }

        ... // rest of the code

        wp_update_post( $my_image_meta );
    }
}
4
  • Yeah works perfectly! Many thanks Krzysiek Dróżdż :)
    – PhineasD
    Commented Jun 5, 2018 at 19:47
  • No problem. Remember to mark answer as correct, if it was helpful - it will help other users to find working solutions for their problems later on. Commented Jun 5, 2018 at 19:50
  • How can I do it? I don't see any button or option.
    – PhineasD
    Commented Jun 5, 2018 at 23:18
  • stackoverflow.com/help/someone-answers Commented Jun 6, 2018 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.