El_toro Posted July 5, 2018 Share Posted July 5, 2018 Hello all, I'm a total newbie to the PHP, and my knowledge is practically close to zero, so please don't judge me for this question. I am using a WordPress plugin called Automatic Featured Image Posts. It does a great job creating a draft post from a newly uploaded image. In the functions.php file, there's this filter: add_filter( 'afip_new_post_content', 'myprefix_change_afip_post_content', 10, 2 ); /* Grabs the image source for the newly created image and inserts it * into the new post content along with a one line paragraph. */ function myprefix_change_afip_post_content( $post_content, $attachment_id ) { $my_uploaded_image = wp_get_attachment_image_src( $attachment_id ); $post_content = '<p>This is my new uploaded image....</p>'; $post_content .= '<img src="' . $my_uploaded_image[0] . '">'; return $post_content; } It works perfectly, but there's one problem - it doesn't add an alt tag. I want the alt tag to be the same as the name of the picture that the plugin uploads to the post. Since I am a total noob I just tried to modify this line just to check out if it works: $post_content .= '<img src="' . $my_uploaded_image[0] . '" alt="' . $attachment_id . '">'; And it did. But it's obviously not the thing I am looking for. It just puts in the number of the attachment. Now, I know that Image's 'alt' text is stored as a string in wp_postmeta with the meta_key of '_wp_attachment_image_alt'. However, I still don't know how should I add the image title as an alt into this code. Quote Link to comment https://forums.phpfreaks.com/topic/307442-alt-image-on-wordpress-plugin/ Share on other sites More sharing options...
requinix Posted July 5, 2018 Share Posted July 5, 2018 Rather than make the <img> manually, it would be better to use wp_get_attachment_image. Quote Link to comment https://forums.phpfreaks.com/topic/307442-alt-image-on-wordpress-plugin/#findComment-1559279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.