Alt image

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
toro1
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Jul 04, 2018 10:47 pm

Wed Jul 04, 2018 10:50 pm

Hello all,

I'm a total newbie to the PHP and I am just learning the basics, so sorry if my question will sound silly.

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:

Code: Select all

    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;
}
So, my question is - how to add an alt image to this code? The alt image should be the same as the title of the new post. Do I have to make another function for this? I know the first thing I have to do is to add alt="" into the tag. But how do I get the title of the post to appear into the alt?

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Jul 06, 2018 10:46 am

Hey,
Yes just add the alt to this line:

Code: Select all

    $post_content .= '<img src="' . $my_uploaded_image[0] . '" alt="Alt text">';

toro1
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Jul 04, 2018 10:47 pm

Sat Jul 07, 2018 3:03 am

Yeah I tried that, but that adds the same alt tag to every image. What I want is that it would be dynamic. For example, I have 3 different images with the following file names: image-one.jpg, image-two.jpg and image-three.jpg. If I add the alt tag as you wrote, I get the same alt for these three images. What I want is to get: alt="image one" for the first one, alt="image two" for the second one, and alt="image three" for the third one.

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Sun Jul 08, 2018 3:34 am

Yes, that was just an example of the HTML, you can replace the alt text with a string from a variable.
But you will need to define that variable or pass it into the function.

Post Reply