Regex to Split Data to Custom Fields
-
Hi,
Has anyone examples of using regex to populate custom fields? One of my IPTC fields (headline) is split as follows
ARTIST || EVENT
I’d like for
ARTIST
to go into a custom field ofdamArtist
andEVENT
to go into a custom field ofdamEvent
-
Thanks for your question. You can use MLA’s “MLA Custom Field and IPTC/EXIF Mapping Actions and Filters (Hooks)” with a small custom plugin to accomplish this task. There is more information in the Settings/Media library Assistant Documentation tab.
In the
/media-library-assistant/examples/
directory you can find “mla-simple-mapping-hooks-example.php.txt”, an example plugin that modifies the Title and ALT Text fields. It would be straightforward to adapt this to update one ore more custom fields.If something along those lines would work for you I can develop more specific code for the example. I will leave this topic unresolved until I hear back from you; if you’d like to proceed, let me know.
Thanks for getting back!
Yes, something like that would be great. I can imagine it would be a very useful tool going forward as well.
For instance, people may not have the tools to edit IPTC/EXIF files and store information in the filename. Being able to split it out by a chosen delimiter and then populate custom fields/normal fields based on each element in the resulting array, would be brilliant.
Also if a particular string matches anothr string e.g. ##### or NA then that value can be ignored…. comes in useful when no value is stored for that image
I have uploaded a new Development Version dated 20150824. It contains an updated example plugin,
/examples/mla-simple-mapping-hooks-example.php.txt
, that contains a solution for your application. It will recognize any IPTC headline containing the ” || ” delimiter and split it into two parts, storing them in custom fields “damArtist” and “damEvent”. Any image that does not have a headline or any headline that does not contain the delimiter is ignored.The code specific to your needs is in one filter; you can use the example plugin or add this code to your theme’s
functions.php
file:public static function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { /* * The first part of the example splits the IPTC "2#105 headline" field, if present, and * updates two custom fields with the resulting values. You must define an IPTC/EXIF * custom field mapping rule for "regex_split_headline" to activate this logic. */ if ( isset( $updates['custom_updates'] ) && isset( $updates['custom_updates']['regex_split_headline'] ) ) { $headline = $updates['custom_updates']['regex_split_headline']; $headline = is_string( $headline ) ? trim( $headline ) : ''; if ( preg_match( '/(.*)\|\|(.*)/', $headline, $matches ) ) { $artist = trim( $matches[1] ); $event = trim( $matches[2] ); /* * You can update the field(s) directly or (preferred) let MLA do the updates */ if ( ! empty( $artist ) ) { //update_metadata( 'post', $post_id, 'damArtist', $artist ); $updates['custom_updates']['damArtist'] = $artist; } else { //delete_metadata( 'post', $post_id, 'damArtist' ); $updates['custom_updates']['damArtist'] = NULL; } if ( ! empty( $event ) ) { //update_metadata( 'post', $post_id, 'damEvent', $event ); $updates['custom_updates']['damArtist'] = $event; } else { //delete_metadata( 'post', $post_id, 'damEvent' ); $updates['custom_updates']['damEvent'] = NULL; } } // We don't actually store regex_split_headline as a custom field unset( $updates['custom_updates']['regex_split_headline'] ); } /* * To stop this rule's updates, return an empty array, i.e., return array(); */ return $updates; } // mla_mapping_updates_filter
To activate the code you must define an IPTC/EXIF mapping rule to populate the
regex_split_headline
value. this field is not actually stored as a custom field but it signals the plugin to run the example code and it supplies the IPTC value to populate the “real” custom fields:- Go to the Settings/Media Library Assistant IPTC/EXIF tab.
- Scroll down to the “Add a new Field and Mapping Rule” section at the bottom of the screen.
- In the Field Title text box enter “regex_split_headline”
- In the “IPTC Value” dropdown, select “2#105 headline”.
- Leave the “EXIF/Template Value” text box blank.
- In the “Priority” dropdown, select “IPTC”.
- In the “Existing Text” dropdown, select “Replace”.
- Leave the “Format: Native” and “Option: Text” defaults in place.
- Check the “Delete NULL values” box.
- Click “Add Field” to record the new rule.
You can find step-by-step instructions for using the Development Version in this earlier topic:
If you get a chance to try the Development Version please let me know how it works for you. If you need more specific help installing and activating the example plugin, let me know. If you would like a copy of the plugin by email, use the Contact Us page at the FTJ web site to send me your contact information:
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.
I am marking this topic resolved, but please update it if you have any problems or further questions regarding the solution presented above. Thanks for your patience and for your interest in the plugin.
- The topic ‘Regex to Split Data to Custom Fields’ is closed to new replies.