Support

Account

Home Forums Add-ons Gallery Field Adding image terms to the side panel in ACF Gallery

Solved

Adding image terms to the side panel in ACF Gallery

  • Hi,
    I have setup a custom taxonomy for images and I am trying to display the taxonomy in the ACF Gallery side panel so that users can easily update the terms when editing a post Gallery.

    So I have a couple questions about this and how to make it work.

    Q1.
    I have managed to get the Image’s Terms to display in the side panel but I had to edit the plugin files. Is there a way to do this and still keep the plugin update safe.
    Here is how I edited the gallery.php file:
    This is in the render_attachment function

    // Image terms
    $terms = get_terms(array(
        'taxonomy' => 'image_cat',
        'hide_empty' => false,
    ));
    
    // Choices Array
    $term_choices = array();
    foreach($terms as $term) {
        $term_choices[$term->slug] = __($term->name, 'acf');
    }
    
    // Render the Options
    acf_render_field_wrap(array(
    	'name'	  => 'terms',
    	'prefix'  => $prefix,
    	'type'	  => 'checkbox',
    	'label'	  => __('Categories', 'acf'),
            'value'	  => $attachment['terms'], // Data from wp_prepare_attachment_for_js
            'choices' => $term_choices, // All choices
    ), 'tr');

    And here I have added a filter to wp_prepare_attachment_for_js to add the terms

    function ggstyle_add_cats_to_prepare_attachment_for_js($response, $attachment, $meta) {
        
        $terms = get_the_terms($attachment, 'image_cat');
        
        if($terms) {
            $response['taxonomy'] = 'image_cat';
            foreach($terms as $term) {
                $response['terms'][] = $term->slug;
            }
        }
    
        return $response;
    }
    add_filter( 'wp_prepare_attachment_for_js', 'ggstyle_add_cats_to_prepare_attachment_for_js', 10, 3 );

    Q2.
    And here is where I am really stuck. I can’t get the updated Terms to save to the image.
    Here is my current code, where I have added a filter to attachment_fields_to_save in the ajax_update_attachment function
    I have tried various things and nothing has worked.

    function ggstyle_acf_update_attachment($post, $changes) {
        
        if( isset( $changes['terms'] ) ) {
            foreach($changes['terms'] as $term) {
                $post['terms'][] = $term;
            }
        }
            	
        wp_set_object_terms($post->ID, $post['terms'], 'image_cat', false);
    }
    add_filter( 'attachment_fields_to_save', 'ggstyle_acf_update_attachment', 10, 2);
  • Hi @nathangreengraphics-com-au

    I believe you can just use the taxonomy field and then set the location rule to “Attachment <=> is equal to <=> All”. Is this what you want? If it is not, could you please share some screenshots of the issue and explain it in more details?

    Thanks 🙂

  • Hi James

    Thanks for your suggestion but I don’t think that is what I am after.

    I already have a taxonomy assign to and displaying for my attachments.
    And I also have the taxonomy displaying in a Gallery field for each Image.

    What I am not able to get working is when I click Update on an Image in the Gallery field the new assigned Terms are not saving.

    I have attached a screen shot of my setup.

  • Hi @nathangreengraphics-com-au

    I believe that’s what my suggestion does. I’ve attached the screenshots of my taxonomy field setup and how it shows up in the gallery. Could you please try it out? If it still not what you want, could you please tell me what’s missing from that method?

    Thanks 🙂

  • Hey James

    My apologies, that does work!
    And so much simpler then what I have been trying…

    Thank you very much for all your help.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.