How do I extract the Alt Text from an image attachment upload via the WordPress Options Framework Plugin?

There are 2 potential solutions to this.

Solution 1: Since Options Framework only gives you an uploader, I assume you are entering the alt text manually on the WP admin side. You could create a text option for the alt text so you can enter it from your custom options panel. Then just echo that option where you need it:

$options[] = array(
    'name' => __('Header Overlay Alt Text', 'options_framework_theme'),
    'desc' => __('Alternate text for your header overlay image.', 'options_framework_theme'),
    'id' => 'header_overlay_alt',
    'type' => 'text');

Then in your template:

<img src="https://wordpress.stackexchange.com/questions/75148/<?php echo of_get_option("header_overlay'); ?>" alt="https://wordpress.stackexchange.com/questions/75148/<?php echo of_get_option("header_overlay_alt'); ?>">

OR

Solution 2: (This one more directly answers your question.)
a) Retrieve the attachment ID based on the URL and b) use that ID to retrieve the corresponding alt text. (Credit for part a goes to Philip Newcomer: http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/.)

function pn_get_attachment_id_from_url( $attachment_url="" ) {

global $wpdb;
$attachment_id = false;

// If there is no url, return.
if ( '' == $attachment_url )
    return;

// Get the upload directory paths
$upload_dir_paths = wp_upload_dir();

// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

    // If this is the URL of an auto-generated thumbnail, get the URL of the original image
    $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

    // Remove the upload path base directory from the attachment URL
    $attachment_url = str_replace( $upload_dir_paths['baseurl'] . "https://wordpress.stackexchange.com/", '', $attachment_url );

    // Finally, run a custom database query to get the attachment ID from the modified attachment URL
    $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value="%s" AND wposts.post_type="attachment"", $attachment_url ) );

}

return $attachment_id;
}

$your_id = pn_get_attachment_id_from_url(of_get_option('header_overlay'));
$alt_text = get_post_meta($your_id, '_wp_attachment_image_alt', true);

Then echo it out as needed:

<img src="https://wordpress.stackexchange.com/questions/75148/<?php echo of_get_option("header_overlay'); ?>" alt="<?php echo $alt_text; ?>">

I know this question is 6 months old, but even if the OP has moved on, hopefully this answer will help someone!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)