Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save attachment ID for upload fields #138

Closed
helgatheviking opened this issue Jan 18, 2013 · 14 comments
Closed

Save attachment ID for upload fields #138

helgatheviking opened this issue Jan 18, 2013 · 14 comments
Labels

Comments

@helgatheviking
Copy link

@helgatheviking helgatheviking commented Jan 18, 2013

Putting this here as a reminder to myself to take a look at it. Ideally I'd like to see the upload fields also save the attachment IDs so that theme creators would have more control over the image's display. Serving different sizes and/or retrieving alt text, etc.

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 19, 2013

Hi Kathy. I think the full URL needs to be saved out into the same field for backwards compatibility reasons.

My thought is that the ID could be saved into an additional field.

E.g., let's say your option id is "uploaded-item". This would still save the URL into it.

Then we could save the image id into "uploaded-item-id", which would be a hidden field in the options panel.

Or, it could be saved it into "uploaded-item-data" as an array with "mime-type" etc.

I haven't done much work on this end, so I'm going to investigate how tricky it is to get data like "mime-type" etc, once you have the image ID. If it is real easy, I'm not sure it would be worth the extra effort to save everything in to the options.

You don't need to do any code work on this (I can take care of it), but your advice on this approach would be welcome.

@helgatheviking

This comment has been minimized.

Copy link
Author

@helgatheviking helgatheviking commented Jan 19, 2013

Well yesterday I was thinking about an approach similar to how you are doing typography and backgrounds... ie: with an array. I think you'd be able to write a short update function that would convert the values. But now I am stuck with how to apply that to of_get_option() as returning an array where people are expecting an URL would lead to a lot of broken images. Unless you added another parameter to of_get_option to return a specific index of any array?

Your idea is pretty sound though and seems like the least amount of work. 👍

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 19, 2013

Yes, the real issue is with of_get_option if it was converted to an array.

A third option would be simply to create a new option. Perhaps instead of type=> 'upload', do type->'upload-file'- but that seems like more code bloat than it is worth (and confusing for people just starting out with the framework).

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 21, 2013

This function works to return the image attachment ID from a URL:

 function prefix_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'] . '/', '', $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;
}

Copied from: http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/

@helgatheviking

This comment has been minimized.

Copy link
Author

@helgatheviking helgatheviking commented Jan 21, 2013

That seems like an expensive function to run every time you want an ID, though I supposed you could save it in the transients and then refresh the transient only on options save. You probably already have a hook for the options getting saved?

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 21, 2013

I started to add the hidden fields to the framework, but the code began to feel bloated with all those extra bits.

I'm thinking this function could be rather self-contained. If it's called, it will first check if the options is saved, if not, it will update_option and create it.

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 21, 2013

This commit breaks a couple things, but I just wanted to get the code up here:
07939cc

I feel like this strategy will work- except updating the options once the ID is found doesn't seem to be actually working:

// We'll save the ID into the options so we don't have to look it up again
        $options[$name . '_id'] = $attachment_id;
        update_option( $config['id'], $options );

Will debug it more later.

@helgatheviking

This comment has been minimized.

Copy link
Author

@helgatheviking helgatheviking commented Jan 21, 2013

Weird. I don't see any reason that it wouldn't save. Also, don't forget that you're going to need to delete/modify the _id version if the image is ever changed. If this function saves the attachment id as its own field in the options array, once the image is changed that _id will still pass is isset test and return the old value.

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented Jan 27, 2013

I pulled this code out again. May try to get this release out the door- and then work through the attachment ID code for the next release.

@helgatheviking

This comment has been minimized.

Copy link
Author

@helgatheviking helgatheviking commented Jan 28, 2013

Ok, sounds good. Thanks for your work on this Devin!

@lonchbox

This comment has been minimized.

Copy link

@lonchbox lonchbox commented May 16, 2013

thanx Devin

@devinsays

This comment has been minimized.

Copy link
Owner

@devinsays devinsays commented May 1, 2014

Was looking at this thread again because it came up as a part of another conversation. I think to maintain compatibility and not break themes the best would be to create a new option "media", that would only store items in the media library. We could also create a separate "url" option for folks that plan to link to external resources.

@achataing

This comment has been minimized.

Copy link

@achataing achataing commented Apr 10, 2015

I have the following

I use the options framework to upload an image and get the url to $imgURL

$imgURL = of_get_option("uploader",get_template_directory_uri()."/img/banner_home.jpg");

I use the function that Davin put above to get the ID of that image (I do not mind about the image resolution because I will work on that later) to $imgID.

$imgID = prefix_get_attachment_id_from_url( $imgURL );

$imgTAG is going to be a string variable that will contains an HTML markup tag such as: . It is a wp function that returns this. Example: . It is a wp function that returns this. The parameter "full" will get the full version, other parameters get lower resolution. refer to the codex for more

$imgTAG = wp_get_attachment_image($epk_image_id,'full');

Now we extract the necessary data from that tag
//$imgATTRIBUTE[0] = src
//$imgATTRIBUTE[1] = width
//$imgATTRIBUTE[2] = height

$imgATTRIBUTE = NlGhT_get_img_attributes ($imgTAG);

/**

  • The following function returns an array with the img attributes
    */
    function NlGhT_get_img_attributes($url){
    //$html = file_get_contents($url);

    $doc = new DOMDocument();
    $doc->loadHTML($url/*$html*/);
    $xpath = new DOMXPath($doc);
    $array = array();
    $array[0] = $xpath->evaluate("string(//img/@src)"); 
    $array[1] = $xpath->evaluate("string(//img/@width)");
    $array[2] = $xpath->evaluate("string(//img/@height)");
    return $array;
    

    }

@tameroski

This comment has been minimized.

Copy link

@tameroski tameroski commented Oct 8, 2015

+1 on that new "media" option. Would be way better for accessing media attributes.

@devinsays devinsays closed this Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.