Add att tags to image with php
-
I’m using the frontend uploader plugin and was able to hook into tagging the post with the user’s inputted tags, but I would also like to copy those tags to the att. tags that MLA provides.
Is there a way to add tags to the image uploaded and have those tags show up in the MLA att. tags section? (at the time the code is executed, the image is already uploaded).
Thanks!
https://www.ads-software.com/plugins/media-library-assistant/
-
Thank you for your question; I want to confirm that I have understood what you have done and what you want to do.
You wrote “hook into tagging the post with the user’s inputted tags“. I understand that to mean you have added images to your Media library and assigned terms in the Tags/post_tag taxonomy to them. Is that right?
You wrote “add tags to the image uploaded and have those tags show up in the MLA att. tags section“. I understand that to mean that you want to copy the terms already assigned to you items from the post_tag taxonomy to the Att. Tags/attachment_tags taxonomy. Is that right?
If I got both of the above right, the process for copying terms from one taxonomy to another is simple. First, you need to set up a mapping rule for the operation. Here are the step-by-step instructions:
- Go to the Settings/Media Library Assistant IPTC/EXIF tab.
- Scroll down to the “Taxonomy term mapping” section.
- Find the taxonomy you want to copy terms into (the destination). In your case, I believe this is “Att. Tags”.
- In the “EXIF/Template Value” text box, enter something like
template:[+terms:post_tag,array+]
, wherepost_tag
is the “slug” for the Tags taxonomy. You can also usecategory
,attachment_tag
orattachment_category
, depending on where the terms currently reside. - In the “Priority” dropdown, enter “EXIF”.
- In the “Existing Text” dropdown, enter “Replace”.
- In the “Delimiter(s)” text box, enter a comma (,).
- If you are copying into the
category
orattachment_category
taxonomy you can pick a “Parent” term in the dropdown on the right. - Scroll down to the bottom of the screen and click “Save Changes”.
Once you define the rule you can apply it to a single image, multiple images or all images:
- To map a single image, go to the Media/Assistant submenu and click the thumbnail of the image you want (or click the “Edit” rollover action) to get the Media/Edit Media screen. You can click the “Map IPTC/EXIF metadata” link to run your rules on this image, then scroll down and look through the taxonomy meta boxes to inspect the results.
- To map two or more images, go to the Media/Assistant submenu and click the checkbox next to the images you want. Then, select “Edit” from the “Bulk Actions” dropdown above the checkboxes and click “Apply” to open the Bulk Edit area. Click the “Map IPTC/EXIF metadata” button to run your rules on the selected images.
- To map all of your images, stay on the Settings/Media Library Assistant IPTC/EXIF tab and click the “Map All Attachments, Taxonomy Terms Now” button. This may take a while.
If this is a one-time task, you should remove the rule after you’re done. If you want to assign Att. Tags terms to items as they are uploaded you can define a replacement rule to get terms from the IPTC or EXIF data in the images.
I hope that gets you the results you want. I am marking this topic resolved, but please update it if you have problems or further questions regarding the use of mapping rules and content templates to copy terms from one taxonomy to another. Thanks for your interest in the plugin.
Thanks for your response!
A couple quick items/questions:
You wrote “hook into tagging the post with the user’s inputted tags”. I understand that to mean you have added images to your Media library and assigned terms in the Tags/post_tag taxonomy to them. Is that right?
Yes – here is my function that is doing that:
add_filter( 'fu_after_create_post', 'my_fu_after_create_post' ); function my_fu_after_create_post( $post_id ) { $term = sanitize_text_field( $_POST['post_tags'] ); wp_set_post_tags( $post_id, $term, true ); }
You wrote “add tags to the image uploaded and have those tags show up in the MLA att. tags section”. I understand that to mean that you want to copy the terms already assigned to you items from the post_tag taxonomy to the Att. Tags/attachment_tags taxonomy. Is that right?
Yes, I want the $_POST[‘post_tags’] (whatever they may be) to show up in the ‘Att. Tags’ section.
I followed your instructions for applying and saving the terms with the following for IPTC/EXIF Taxonomy term mapping in Attr. Tags:
- IPTC Value: – None (select a value) –
- EXIF/Template Value: template:[+terms:post_tag,array+]
- Priority: EXIF
- Existing Text: Replace
- Delimiter(s): ,
Then, I applied them to a single image and my new tags for the image where:
- [+terms:post_tag
- array+]
Does everything after template have to be the exact name of the tags have to be comma separated (can’t grab them dynamically)?
In the long run, yes we definitely want this to be done after the items are uploaded without any admin interaction. If we wanted to do this, would I just click the checkbox that says “Enable IPTC/EXIF Mapping when adding new media”?
Thanks again for your help!
Thanks for your update with the details of your application. I am not sure where the
'fu_after_create_post'
filter is defined; it is not a WordPress filter, so it may be part of your Theme or another plugin. Is that right?The instructions I gave in my first response are for copying terms already present in the
post_tags
taxonomy to the Att. Tags taxonomy. The template I gave you has an unfortunate flaw, which your tests have exposed. If there are nopost_tags
terms assigned to the item the template returns[+terms:post_tag,array+]
as a literal value, which gets interpreted as two terms separated by a comma. I regret this error.You can fix the template to return nothing when there are no
post_tags
. Simply add parentheses around the substitution variable, i.e., code the template astemplate:([+terms:post_tag,array+])
.However, this is not very useful for your application. The code you posted is taking terms from a variable sent in by the browser,
$_POST['post_tags']
, and using a WordPress function to copy them into thepost_tags
taxonomy. Depending on when the filter runs the terms may not be there when the MLA mapping rule runs.Since you are already running some PHP code in the filter, the most direct way to accomplish your goal is to simply replace the
wp_set_post_tags( $post_id, $term, true );
function call with a call that puts the terms into the Att. Tags taxonomy in the first place. You can try this alternative function call:wp_set_object_terms( $post_id, $term, 'attachment_tag', true );
Of course, you should read the wp_set_object_terms Function Reference to make sure you understand what it does.
If you can get the filter you already have working it is the simplest solution. If not, give me more information about the source of the
'fu_after_create_post'
filter and I will see if there is another solution for your application. Thank you for your understanding and your patience.
- The topic ‘Add att tags to image with php’ is closed to new replies.