Get img alt tag from a Image that has been uploaded through the Customizer

So inside this theme I’m making for a client there is a section inside the customizer that allows them to change a header on each individual page, my question is how do I fetch the alt tag of that Image?

The Images are uploaded through the Media Library so the alt text has been set after the Image was uploaded.

Below is what I have tried to fetch the Image alt tag

$img_id = get_post_thumbnail_id(get_the_ID());
$alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true);

<img src="https://wordpress.stackexchange.com/questions/221481/<?php echo get_theme_mod("about-header', get_stylesheet_directory_uri() . '/assests/imgs/placeholder.png'); ?>" alt="<?php echo $alt_text; ?>">

For some reason it just doesn’t seem to get the Images alt tag and I don’t understand why

Code for the Customizer (just in case I have to do something inside there)

$wp_customize->add_section('page_header', array(
    'title'         => __('Page Headers', 'bissell-theme'),
    'priority'      => 30,
    'description'   => __('Below are the options to change your header images for all your pages. Just simply click \'Select Image\' and choose a Image from the Media Libary or Upload one your self'),
));

$wp_customize->add_setting('about-header');

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'about_header_control', array(
    'label'         => __('About Us - Header'),
    'section'       => 'page_header',
    'settings'      => 'about-header',
)));

2 Answers
2

Ok I found the answer that no one has on the net I been looking for days now.

Here is how I was able to do it. Hope this helps someone out there

// This is getting the image / url
$feature1 = get_theme_mod('feature_image_1');

// This is getting the post id
$feature1_id = attachment_url_to_postid($feature1);

// This is getting the alt text from the image that is set in the media area
$image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );

Markup

<a href="https://wordpress.stackexchange.com/questions/221481/<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>

Leave a Comment