Media Library Modal – Show Featured/Inserted In
-
Hello,
When setting a featured image in a post – after clicking on an image in the grid, the attachment details show to the right. You already include the Att. Categories and Att. Tags.
Is it possible to show the Featured In and Inserted In values that you have for the image?
Or is there a way in the modal with MLA to filter in/out images that are already used (featured in, inserted in, or both)?
The use case for this is I have multiple authors and I upload hundred of photos for them to use (as I want to make sure they are not breaking copyright laws) and ideally only want them to use images that have not been used before. In another case, images that I have purchased can only be used one time, so in that case, if they do reuse it, having the name of the posts that it was used in already is handy to remove it from that post.
Thanks,
Jon
-
FYI… I was able to partially get the featured in/inserted in fields to show by creating custom fields for media and then using MLA’s custom field mapping to map your fields to my custom fields. But the problem there is when an image is set as a featured image for a post, your fields get updated properly but my custom fields do not unless I run the map all fields via MLA settings.
Any suggestions, workarounds, or customizations is appreciated.
Thank you for your questions and for the detailed and thoughtful use case; very helpful.
In your second post you wrote “I was able to partially get the featured in/inserted in fields to show by creating custom fields for media … Does that mean your custom fields are displayed in the Media Manager Modal (popup) Window? Can you say more about how you created the custom fields and have them show up in the ATTACHMENT DETAILS pane?
Creating the “where-used” information, e.g., Featured In and Inserted In, is a database-intensive process and MLA avoids the work whenever possible. There is some MLA code for updating the information when posts/pages are changed, but nothing that keeps custom field mapping rules in synch. I’m not sure I could implement a practical, general solution to that problem.
For your application the extra processing time can be justified. It should be possible to use some combination of WordPress and MLA hooks (filters and actions) to do the work. I will do some investigation and post an update when I have progress to report. If you can tell me more about your custom fields, any additional information would be helpful. Thanks for an interesting question and for your patience.
Hi David,
I created the custom fields using the Advanced Custom Fields plugin… they were just text fields. MLA’s Att. Category and Att. Tag fields show up in the modal attachment details pane too.
Anything you come up would be great… thanks!
Jon
Jon,
Thank you for your patience. I have not forgotten this topic, but I have been very busy updating MLA to be compatible with the coming WordPress 4.3 release, now scheduled for August 18.
I am thinking through your requirements and a practical way of satisfying them. You wrote: “is there a way in the modal with MLA to filter in/out images that are already used “?
MLA has a couple of features that make that possible. You can create a custom field and a mapping rule that filters by “already used”:
- Field Title: “Used In”
- Data Source: Template
- Template text box: “([+featured_in+]) ([+inserted_in+])”
- Existing Text: Replace
- Format: Native
- Option: Text
- Delete NULL values: checked
You can use the custom field to create one (or two) new Media/Assistant Views. Go to the Settings/Media library Assistant Views tab and add a view for all items that are not used:
- Slug: not-used
- Singular Label: Available
- Plural Label: Available
- Specification: “custom:Used In,null”
- Post MIME Type: unchecked
- Table View: checked
Then click “Add View” to save your work. Now you have a new view on the Media/Assistant submenu table and in the “All Media Items” dropdown control in the Media Manager Modal (popup) Window. You can create the complementary view as well:
- Slug: used-in
- Singular Label: Used In
- Plural Label: Used In
- Specification: “custom:Used In”
- Post MIME Type: unchecked
- Table View: checked
You can modify the rules you already have for your ACF fields or create new fields.
That doesn’t answer the “your fields get updated properly but my custom fields do not” part of the problem. The straightforward solution here is to automatically re-run the mapping rule(s) whenever the page/post content or Featured Image is changed. That can be done with the hooks already provided in MLA, but its performance depends on the frequency of changes and the number of posts/pages as well as Media Library items. If you go to the Settings/Media Library Assistant Custom Fields tab and click “Map All Attachments” for the rule(s) you have, how long does it take? Is it acceptable to add that time to the time required to save an update to a post or page? If it is, I can work on a simple solution. If not, the code required will be more complicated.
Let me know if the above custom view suggestion is of interest, and what you think about the performance of the straightforward solution. Any additional information you can provide will be helpful.
Hi David,
Your suggested custom field and views work but unfortunately, the “Map All Attachments” took about 15 minutes (we currently have 6989 posts and 1920 images with more of each added every day).
So, the straightforward solution would not be feasible–if I’m understanding you correctly that it would take that long each time we add a new post or set a featured image.
If you can come up with a solution, that would be great but I understand it probably is not a priority.
I was also just thinking of an alternative that would sort of meet my needs. You have an option “Enable Edit Media additional meta boxes” that exposes the featured in and inserted in metadata boxes in the Edit Media window. Would it be possible to expose those fields in the sidebar of the set featured image modal after an image is clicked? It’s a little more work as my authors would still need to click on an image to determine if it’s used or not used but it would still save some time.
Thanks!
Jon
I have released my latest MLA version in preparation for the upcoming WordPress 4.3 update. With that done I am having a look at a more optimized solution to your “dynamic where-used” requirements.
Can you tell me the name(s) of your custom fields, post one or more examples of their content and post the template you use in your mapping rule(s)? That will help me replicate your logic as closely as I can.
It would also be more efficient to communicate by email so I can send you experimental copies of my work as it evolves. Can you send me your contact information using the Contact Us page at the FTJ web site?
Do not post your e-mail address in the forum; personal details in a public forum violates WordPress guidelines. If you have trouble accessing the FTJ site, post a note here with your country of origin and I can temporarily unblock it.
Thanks for your help with this new example and for your interest in the plugin.
Hi David,
I used your FTJ contact form to provide an update and my email address.
Thanks,
JonThank you for working with me offline to develop and test the
mla-dynamic-references-example.php.txt
example plugin, and for testing the MLA updates required to make everything work correctly. The MLA changes were added to version 2.15, and the example plugin will be in the next version after that.This post contains a summary of the solution we worked out.
Define ACF Fields
Since you wanted to display the information in the Media Manager Modal (popup) Window you defined the field(s) with the Advanced Custom Fields plugin. Here’s a complete set that matches the plugin:- Field Group: Where used
- Field Label: Featured, Field Name: featured, Field Type: Text
- Field Label: Featured in, Field Name: featured_in, Field Type: Text
- Field Label: Inserted, Field Name: inserted, Field Type: Text
- Field Label: Inserted in, Field Name: inserted_in, Field Type: Text
- Field Label: Used, Field Name: used, Field Type: Text
- Field Label: Used in, Field Name: used_in, Field Type: Text
- Rules: Show this field group if Attachment is equal to All
You only need to define the fields required for the application. Your application only required the “Used” field.
Define Mapping Rules
Next, define mapping rules so you can initialize the field(s) content. Here are step-by-step instructions for the “Used” field; the other rules are similar:- Navigate to the Settings/Media Library Assistant “Custom Fields” tab.
- Scroll down to the “Add a new Mapping Rule” area, since you already have the custom field defined.
- In the first dropdown box, select your field name, “used”.
- From the Data Source dropdown list, select “- Template (see below) -“.
- In the text box below the dropdown enter “([+featured_in_title+]) ([+inserted_in_title+])”. That populated the field when the item is used as a Featured Image or is inserted in the body of a post/page.
- In the “Existing Text” dropdown list, select “Replace”.
- In the “Format” dropdown list, select “Native”.
- Click the “MLA Column” check box if you want to make the field available in the Media/Assistant submenu table. If you want to edit the field manually, check the “Quick Edit” and perhaps the “Bulk Edit” checkbox as well.
- In the “Option:” dropdown list, select “Text”.
- Check the “Delete NULL Values” checkbox to save database space for items that are not used in any post/page.
- Click the “Add Rule” button to save your work and run the rule later.
Once the rule is in place you can use the Media/Edit Media screen to test it on one item or the Media/Assistant Bulk Edit area to test it on a small number of items. When you are satisfied with it, use the “Map All Attachments” button to run the rule for all your items and complete the work.
For the other fields (if needed) only the name and template values are different. Here they are:
- Field Name: featured, Data Source: featured_in_title
- Field Name: featured_in, Data Source: featured_in
- Field Name: inserted, Data Source: inserted_in_title
- Field Name: inserted_in, Data Source: inserted_in
- Field Name: used, Template: “([+featured_in_title+]) ([+inserted_in_title+])”
- Field Name: used_in, Template: “([+featured_in+]) ([+inserted_in+])”
You can see that only the “Used” and “Used in” fields require a Content Template; the other fields only require the appropriate Data Source. The “Used” field just shows the Titles for each reference, the “Used in” field also shows post type and ID information.
Define Custom Views
Once the fields are set up you can define custom views to filter the Media/Assistant submenu table. These views are also available in the Media Manager Modal Window, in the “All Media Items” dropbox in the upper left corner of the window. To define the views:- Navigate to the Settings/Media Library Assistant “Views” tab.
- In the “Add New View” area on the left-hand side of the screen, enter a unique slug such as “custom-not-used”.
- Enter “Not Used” for the singular and plural label values.
- Enter “custom:used,null” in the Specification text box.Note that “used” begins with a lowercase “u” to match the ACF custom field name (not the field title)
- Uncheck the Post MIME Type box.
- Check the Table View box.
- Leave the Menu Order and Description fields blank.
- Click “Add View” to save your work.
For your second custom view, repeate the above steps but:
- Enter “custom-used” in the Slug text box.
- Enter “Already Used” for the singular and plural label values.
- Enter “custom:used=*” in the Specification text box.
Adapt and Install the Plugin
Finally, adapt the custom plugin to match the field(s) you are actually using, since all six fields are active by default. Towards the top of the file you will see:/** * Names of the custom field(s) to be maintained * * Enter the name of a custom field in each category you want to maintain. * Enter an empty string or comment out each category you don't use. * * Set 'used_first_feature' and/or 'used_first_insert' to true if you want * them to be the default selection in the Media Manager dropdown control. * * @since 1.00 * * @var array */ private static $field_names = array( 'featured' => 'featured', 'featured_in' => 'featured_in', 'inserted' => 'inserted', 'inserted_in' => 'inserted_in', 'used' => 'used', 'used_in' => 'used_in', 'used_first_feature' => true, 'used_first_insert' => true, );
Just put two slash marks in front of any field you are not using. For example, if you are only using the “used” field:
private static $field_names = array( //'featured' => 'featured', //'featured_in' => 'featured_in', //'inserted' => 'inserted', //'inserted_in' => 'inserted_in', 'used' => 'used', //'used_in' => 'used_in', 'used_first_feature' => true, 'used_first_insert' => true, );
As a bonus, you can set the “Available” view as the default that comes up when you open the MMMW. The last two entries in the $field_names array control this decision. Set them to “true” to make the Available view the default, or “false” to use the WordPress “All”/”Images” defaults.
Once that’s done, upload and activate the plugin to automatically update the custom field(s) when pages and posts are edited. The plugin will detect items inserted in (or removed from) the body of the post/page and Featured Image add/change/delete updates.
Since the current MLA version, 2.15, supports this example plugin and you already have the example plugin installed and working I am marking this topic resolved. Thanks again for working with me on this great illustration of MLA’s capabilities.
- The topic ‘Media Library Modal – Show Featured/Inserted In’ is closed to new replies.