Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/307442-alt-image-on-wordpress-plugin/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.