wpalchemy icon indicating copy to clipboard operation
wpalchemy copied to clipboard

Getting title attribute from an uploaded image

Open jackmahoney opened this issue 12 years ago • 2 comments

Hi. Firstly LOVE your class - great work.

I need to get the title and alt attributes of an uploaded image using the MediaAccess class. However it only returns the url. I hacked it to return the title but it isn't such an elegant solution.

Any ideas how to make it better?

Here is my child class https://gist.github.com/3214107

Here is the implementation in a metabox https://gist.github.com/3214109

Thanks

jackmahoney avatar Jul 31 '12 05:07 jackmahoney

Currently MediaAccess can only get the URL of a given media. I wish it would also return all the associated information of the media along (attachment ID, title, alt, description, width, height, etc.).

Perhaps you could give it a shot by forking the project?

saddam-azad avatar Aug 06 '12 12:08 saddam-azad

I extended your WPAlchemy_MediaAccess class and overrode line 329. Perhaps this is of some assistance?

                    send_to_editor = function(html)
                    {
                        clearInterval(interval);

                        if (wpalchemy_mediafield)
                        {
                            var src = html.match(/src=['|"](.*?)['|"] alt=/i);
                            src = (src && src[1]) ? src[1] : '' ;

                            var href = html.match(/href=['|"](.*?)['|"]/i);
                            href = (href && href[1]) ? href[1] : '' ;

                            var url = src ? src : href ;

                            //!important
                            var title = html.match(/title=['|"](.*?)['|"]/i);
                            title = (title && title[1]) ? title[1] : '' ;

                            wpalchemy_mediafield.val(url);
                            wpalchemy_mediafield_metafield.val(title);

                            // reset insert button label
                            setInsertButtonLabel(wpalchemy_insert_button_label);

                            wpalchemy_mediafield = null;
                        }
                        else
                        {
                            wpalchemy_send_to_editor_default(html);
                        }

                        tb_remove();
                    }

jackmahoney avatar Oct 01 '12 08:10 jackmahoney