hsz
hsz

Reputation: 152216

WordPress Gallery with tag support

I am going to implement gallery filtering based on tags. However there is no way to tag images in WordPress.

Is it possible to extend WordPress Gallery with custom taxonomy to specify tags for each image attached do the gallery ?

Upvotes: 0

Views: 93

Answers (1)

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

There are many plugins to do just that , but if you want the whole explanation : https://wordpress.stackexchange.com/questions/76720/how-it-is-possible-to-use-taxonomies-on-attachments

However , there is another way to achieve what you want (and a bit more ) - even if it is not "native" and that is to use custom fields on media .

now do not get me wrong , I am not a big fan of custom fields , and I always think that people "abuse" them , but in this case , they have worked for me in the past ...

/******************************
* ADD CUSTOM FIELDS TO MEDIA ITEMS
* If you really want you can make it a stand-alone plugin. 
* I guess you know how ..
******************************/

    class o99_CustomMediaFields {
        // your array of fields 
        private $aOptions = array('normal' => 'Normal Image', 'polaroid' => 'Polaroid Image', 'bw' => 'Black & White Image', 'beauty' => 'Beauty Image', 'photoset' => 'Photoset');
        function __construct() {
            add_filter('attachment_fields_to_edit', array(&$this, 'handleAttachmentFields'), 10, 2);
            add_action('edit_attachment', array(&$this, 'handleAttachmentSave'));
            add_filter('manage_media_columns', array(&$this, 'handleColumnAdd'));
            add_action('manage_media_custom_column', array(&$this, 'handleColumnContent'),10,2);
        }
        public function handleAttachmentFields($form_fields, $post) {
            foreach($this->aOptions as $sKey => $sName) {
                $bOpt = (bool)get_post_meta($post->ID, 'image_'.$sKey, true);
                $form_fields['image_'.$sKey] = array(
                    'label' => __('Is ').$sName,
                    'input' => 'html',
                    'html' => '<label for="attachments-'.$post->ID.'-'.$sKey.'"> <input type="checkbox" id="attachments-'.$post->ID.'-'.$sKey.'" name="attachments['.$post->ID.']['.$sKey.']" value="1"'.($bOpt ? ' checked="checked"' : '').' /> </label>',
                    'value' => $bOpt,
                );
            }
            return $form_fields;
        }
        public function handleAttachmentSave($attachment_id) {
            foreach($this->aOptions as $sKey => $sName) {
                if (isset($_REQUEST['attachments'][$attachment_id][$sKey])) {
                    add_post_meta($attachment_id, 'image_'.$sKey, '1', true) or update_post_meta($attachment_id, 'image_'.$sKey, '1');
                } else {
                    add_post_meta($attachment_id, 'image_'.$sKey, '0', true) or update_post_meta($attachment_id, 'image_'.$sKey, '0');
                }
            }
        }
        public function handleColumnAdd($columns) {
            $columns['image_type'] = 'Image Type';
            return $columns;
        }
        public function handleColumnContent($column_name, $id) {
            if ($column_name != 'image_type') { return; }
            $sType = '';
            foreach($this->aOptions as $sKey => $sName) {
                $bOpt = (bool)get_post_meta($id, 'image_'.$sKey, true);
                if ($bOpt) { $sType .= $sName.', '; }
            }
            if (strlen($sType) >= 2) { $sType = substr($sType,0,-2); }
            echo $sType;
        }
    }
    new o99_CustomMediaFields();

Upvotes: 1

Related Questions