Thanks for posing an interesting question and stimulating some improvements in my “example plugins”. You are on the right track; using the “MLA Custom Field and IPTC/EXIF Mapping Actions and Filters (Hooks)” is the way to go.
During my research on a solution I realized that the current mla-mapping-hooks-example.php.txt
plugin is too complicated and confusing. I have renamed that example to the mla-metadata-mapping-hooks-example.php.txt
plugin and started over, removing all the code required for that more complex example.
All of the code you need can be put in one place; the mla_mapping_updates_filter()
function. This function is called once, after all of the mapping rules have been evaluated and just before any updates are applied to the item. Adding your code here ensures that your cleaned-up Title value will be applied regardless of where the original Title values comes from. Here’s the code I came up with:
/*
* If $updates[ 'post_title' ] is set, some mapping rule
* has been set up, so we respect the result. If not,
* use whatever the current Title value is.
*/
if ( isset( $updates[ 'post_title' ] ) ) {
$old_value = $updates[ 'post_title' ];
} else {
$post = get_post( $post_id );
$old_value = $post->post_title;
}
/*
* Clean up the Title value. If the cleanup has changed the value,
* put the new value in the $updates array.
*/
$new_title = str_replace( array( '-', '_', '.' ), ' ', $old_value );
if ( $old_value != $new_title ) {
$updates[ 'post_title' ] = $new_title;
}
// Only replace ALT Text if Image Metadata is present
$old_value = get_metadata( 'post', $post_id, '_wp_attachment_metadata', true );
if ( ! empty( $old_value ) ) {
// Find the current ALT Text value
if ( isset( $updates[ 'image_alt' ] ) ) {
$old_value = $updates[ 'image_alt' ];
} else {
$old_value = get_metadata( 'post', $post_id, '_wp_attachment_image_alt', true );
}
// Replace the ALT Text value with the clean Title
if ( $old_value != $new_title ) {
$updates[ 'image_alt' ] = $new_title;
}
}
As you can see:
- The Title value comes from either a mapping rule or from the existing value.
- The cleanup replaces dashes, underscores and periods with spaces; you can add any other changes you need.
- Changes are only applied if the value has actually changed.
- The ALT Text is only examined if there is other image metadata set for the item. This prevents setting ALT Text values for non-image items such as PDF documents.
If you go to the Settings/Media Library Assistant IPTC/EXIF tab and check the “Enable IPTC/EXIF Mapping when adding new media” box (scroll down and click “Save Changes”) the code will automatically run for new uploads. You can apply the rule to a single item by going to the Media/Edit Media page for the item and clicking the “Map IPTC/EXIF Metadata” link in the Save meta box at the upper right corner of the screen. You can also apply the rule to several items at once using the Bulk Edit feature of the Media/Assistant submenu. Adding it to the Media Manager Modal Window (as in your screen shot) is not likely to happen given the complexity of that work.
I will add this code to the example plugin in my next MLA version. If you would like a complete copy of the example plugin with the added code before the next version goes out, let me know. You can give me your e-mail address and other contact information by visiting the Contact Us page at our web site:
Fair Trade Judaica/Contact Us
I am marking this topic resolved, but please update it if you have any problems or further questions on the approach I’ve outlined. Thanks for an interesting question and for your interest in the plugin.