• Resolved karidemi

    (@karidemi)


    Hello,

    First off, this is an excellent plugin and I continue to be impressed with the level of detail in the documentation and features.

    And now for my question:

    I’m using the Restrict Categories plugin to limit what Pages a user can see when they are logged in. I have successfully extended the “filter_media_modal_query_final_terms” filter to only display uploads that are in a particular category when a user views the “media library” screen on a per-page upload. What I would really like to do is programmatically assign a category when a media item is uploaded, which would take the burden of assigning the proper category off of the end-user and prevent users from wondering why they can no longer see items they have uploaded in the event the correct category was not selected.

    I poked around in the MLA example files looking for a filter that would allow me to accomplish this, but I didn’t see anything and thus have finally resorted to asking for help. 🙂

    Thanks for your time.

    https://wordpress.org/plugins/media-library-assistant/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your positive feedback and for your question. Thanks as well for taking the time to dig through the Documentation and example plugin files.

    You mentioned the “filter_media_modal_query_final_terms” filter, which I understand to mean that you are hooking the “mla_media_modal_query_final_terms” filter. Is that right?

    You can programatically assign taxonomy terms and make other changes to newly-uploaded items by hooking MLA’s mla_update_attachment_metadata_prefilter filter. Here is some code you can adapt to suit your application:

    /**
         * @param    array    attachment metadata
         * @param    integer  The Post ID of the new/updated attachment
         * @param    array    Processing options, e.g., 'is_upload'
         *
         * @return    array    updated attachment metadata
         */
        function mla_update_attachment_metadata_prefilter_filter( $data, $post_id, $options ) {
    
            if ( $options['is_upload'] ) {
                // array of int = hierarchical, comma-delimited string = non-hierarchical.
                $term_ids = array( 123, 456 );
                $result = wp_set_post_terms( $post_id, $term_ids, 'attachment_category' );
            }
        }

    You will have to change the $term_ids values and perhaps the taxonomy name, but you get the idea. Be sure to read the Function Reference/wp set post terms to see how it works.

    There are two ways a user can upload new items; the “Add Media” button on a post/page and the Media/Add New (Upload New Media) screen. If your users use the Media/Add New screen you may have to hook additional MLA filters because they can assign terms in the Bulk Edit area on that screen.

    I hope that gives you the information you need to complete your application. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions. Thanks for your interest in the plugin.

    Thread Starter karidemi

    (@karidemi)

    Ah, yes, I meant to say, I hooked into the “filter_media_modal_query_final_terms” filter.

    I will experiment with this today, but based on your response it looks like this is precisely what I needed.

    Thank you!

    Thread Starter karidemi

    (@karidemi)

    Sorry to bug, but I’ve been trying to get this filter to fire, and I’m having a bit of trouble.

    I’m hooking into the mla_update_attachment_metadata_prefilter and just trying to use the example error_log output, to see what I can modify, but I never see anything in my log file. I am assuming the filter should fire whenever a new item is uploaded, is that true? I’ve tried uploading from the media modal and from the Media/Add New screen and I’m still not getting any output.

    Thanks!

    Thread Starter karidemi

    (@karidemi)

    Ok, I now realize that the mla_update_attachment_metadata_prefilter is only firing on the Bulk Edit screen. What I’m really looking for is a way to modify uploads as they come in through the media modal, as that is the only way my users can upload items, at the moment.

    Thanks again for your time.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your updates and for your experiments with the filters.

    The first thing to check is the settings on the Settings/Media Library Assistant Custom Field and IPTC/EXIF tabs. the is only applied when at least one of the four “Enable mapping when adding new media/updating media metadata” options is checked, and the default setting for all four options is unchecked.

    I just ran a quick test and the filter is called from the Upload Files tab of the Media Manager Modal (popup) Window when the mapping options are checked.

    If that doesn’t solve the problem, any additional information you can provide will help my further investigation.

    Thread Starter karidemi

    (@karidemi)

    Thank you for your quick response.

    After upgrading to version 2.22 from 2.21, I am now seeing that filter running on all upload screens as expected. Thanks again!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update and the good news about your success. I looked back at the v2.22 Chang Log and found this item:

    * Fix: When new Media LIbrary items are uploaded via the Media Manager Modal (popup) Window, mapping rule execution has been restored.

    I think that explains it!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Automatic category selection on upload’ is closed to new replies.