I'm using option tree for my theme options page. But I can't retrieve uploaded images attribute like alternative text, description,caption etc.

How can I do that.

  • what does option tree store, an attachment ID?
    – Milo
    Feb 20 '16 at 5:10
  • Hello, #Milo , Thank u for you quick replie.I'm a newbee.did you mean " 'class' => 'ot-upload-attachment-id' " to use. yes i did it. Feb 20 '16 at 5:31
  • no, I mean how is your option field storing the reference to an image? is it an attachment ID, or just a URL?
    – Milo
    Feb 20 '16 at 17:58
  • Sorry for late @Milo, I don't know exactly, I think it referencing just URL because I retrieved image src with just " $header_logo = get_option_tree( 'header_logo', '', false ); " . How can I know what option field storing. Feb 23 '16 at 4:28

Put this code in index.php

<img class="img-resposive" src="<?php 
    $logo=get_option_tree( 'logo','','true'); // return src of img
    $id_logo = get_attachment_id_from_src($logo); // This is custom  function for getting image id.
    $alt = get_post_meta($id_logo, '_wp_attachment_image_alt', true);// get alt="" from wordpress media.
    if ( function_exists( 'get_option_tree') ) : 
        if( get_option_tree( 'logo')) :             
             echo bloginfo('template_directory') .'/assets/images/logo_vp.png'; // else if option is empty get this image.
?>" alt="<?php echo $alt; ?>"/>

Put this function code in functions.php

//get id from image source
function get_attachment_id_from_src ($image_src) {
    global $wpdb;
    $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    $id = $wpdb->get_var($query);
    return $id;

This code is tested. Working fine.


Here is the correct answer for optiontree 2.4+



'class'       => 'ot-upload-attachment-id',


  'id'          => 'demo_upload_attachment_id',
  'label'       => __( 'Upload Attachment ID', 'option-tree-theme' ),
  'desc'        => sprintf( __( 'The Upload option type can also be saved as an attachment ID by adding %s to the class attribute.', 'option-tree-theme' ), '<code>ot-upload-attachment-id</code>' ),
  'std'         => '',
  'type'        => 'upload',
  'section'     => 'option_types',
  'rows'        => '',
  'post_type'   => '',
  'taxonomy'    => '',
  'min_max_step'=> '',
  'class'       => 'ot-upload-attachment-id',
  'condition'   => '',
  'operator'    => 'and'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.