1

I'm trying to get the alt text of the custom logo specified in the theme customization. Unfortunately this doesn't work because it seems the alt text is not included in the post meta:

$custom_logo_id = get_theme_mod( 'custom_logo' );
get_post_meta($custom_logo_id, '_wp_attachment_image_alt', true);

I would have used get_custom_logo which includes the alt text, but I'm building my own custom srcset and sizes attributes, and need ONLY the alt text to add to it.

What other options do I have?

1 Answer 1

0

I know this question is pretty old, but I needed this today. What I ended up doing was running the get_custom_logo_image_attributes filter, storing the array provided within it when it gets run, and accessing that afterward.

In functions.php:

/* custom logo attribute collection */
add_filter( 'get_custom_logo_image_attributes', function($custom_logo_attr, $custom_logo_id, $blog_id ) {
    global $logo_attrs;
    $logo_attrs = $custom_logo_attr;
    return $custom_logo_attr;
}, 10, 3);

function get_custom_logo_image_attributes() {
    global $logo_attrs;
    return $logo_attrs;
}

Then when it's time to get the data, you have to first be sure the filter has run:

get_custom_logo(); // ensure logo filter is called before our function
$logo_attrs = get_custom_logo_image_attributes();

Once you have that, you can use $logo_attrs['alt'] where you need it.

It's not pretty, but it got the job done.

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.