set title to be filename stripping characters
-
Hi!
Great plugin!
I use this topic in order to set the title to the filename
Imagine a filename like this:Sprue_WJ-3.jpg
how can I make it strip all the hyphens (replace with space), underscores (replace with space) and extensions? so it would end up likeSprue WJ 3
Thanks!
https://www.ads-software.com/plugins/media-library-assistant/
-
Thanks for the positive feedback and for your question. As you found in the earlier topic you referenced, MLA has a number of “data sources” you can use to compose new values for fields like the Title. However, to “clean up” the values or to make more complex changes requires a bit of PHP code, easily placed in your theme’s
functions.php
file or a small custom plugin. In fact, similar requests have come before. This earlier topic is very close to yours:Quick question on replacing string(s) in image metadata
Another topic describes a very small custom plugin that creates Title values containing an active link based on an IPTC value:
The code in the second example is somewhat smaller than the first. For your specific application, it can be even simpler. Here is the complete source code of a custom plugin for your application:
<?php /** * Replaces the Title by a cleaned up version of the file name. * * @package MLA File Name Mapping Hooks Example * @version 1.00 */ /* Plugin Name: MLA File Name Mapping Hooks Example Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Replace Title with hyperlink, for Jeff Dean. Author: David Lingren Version: 1.01 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2014 - 2016 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA File Name Mapping Hooks Example hooks one of the filters provided by the IPTC/EXIF and Custom Field mapping features * * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding enerything * else inside a class means this is the only name you have to worry about. * * @package MLA File Name Mapping Hooks Example * @since 1.00 */ class MLAFileNameMappingHooksExample { /** * Initialization function, similar to __construct() * * Installs filters and actions that handle the MLA hooks for uploading and mapping. * * @since 1.00 * * @return void */ public static function initialize() { /* * The filters are only useful in the admin section; exit if in the "front-end" posts/pages. */ if ( ! is_admin() ) return; /* * add_filter parameters: * $tag - name of the hook you're filtering; defined by [mla_gallery] * $function_to_add - function to be called when [mla_gallery] applies the filter * $priority - default 10; lower runs earlier, higher runs later * $accepted_args - number of arguments your function accepts * Comment out the filters you don't need; save them for future use */ add_filter( 'mla_mapping_updates', 'MLAFileNameMappingHooksExample::mla_mapping_updates_filter', 10, 5 ); } /** * MLA Mapping Updates Filter * * This filter is called AFTER all mapping rules are applied. * You can add, change or remove updates for the attachment's * standard fields, taxonomies and/or custom fields. * * @since 1.00 * * @param array updates for the attachment's standard fields, taxonomies and/or custom fields * @param integer post ID to be evaluated * @param string category/scope to evaluate against: custom_field_mapping or single_attachment_mapping * @param array mapping rules * @param array attachment_metadata, default NULL * * @return array updated attachment's updates */ public static function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { /* * We are only concerned with Standard Field mapping */ if ( ! in_array( $category, array( 'iptc_exif_mapping', 'iptc_exif_standard_mapping' ) ) ) { return $updates; } /* * 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; } /* * Derive the new Title from the file name (without the extension). * You can use MLAOptions::mla_get_data_source() to get anything available. */ $my_setting = array( 'data_source' => 'template', 'meta_name' => '([+name_only+])', 'option' => 'raw' ); $name_only = trim( MLAOptions::mla_get_data_source( $post_id, 'single_attachment_mapping', $my_setting, NULL ) ); if ( ! empty( $name_only ) ) { $new_title = str_replace( array( '-', '_' ), ' ', $name_only ); if ( $old_value != $new_title ) { $updates[ 'post_title' ] = $new_title; } } return $updates; } // mla_mapping_updates_filter } //MLAFileNameMappingHooksExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAFileNameMappingHooksExample::initialize'); ?>
If you put the above code in a file called, for example,
mla-file-name-mapping-hooks-example.php
, install the file in yourpluging
directory and activate it, you can re-map your existing items and apply the cleanup to new items as well.I am marking this topic resolved, but please update it if you have problems or further questions regarding the above solution. Thanks for your interest in the plugin.
Wow, thanks for your great reply David!
according to your topic link I came up with this as I do not want an extra plugin if not really neccesary:
function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { /* * 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; } } return $updates; }
I put in in my functions.php
it’s not working unfortunately, do you know why?
I have setTitle
,ALT Text
andCaption
totemplate:([+file_name+])
with priorityEXIF
.obviously Im missing something but I have no clue yet.
another question, what exactly do you mean by
// Only replace ALT Text if Image Metadata is present
, does that mean it only replaces the ALT if it is populated?To be clear< I would like Title, ALT and Caption all replaced with the cleaned up filename when mapping.
Thanks David!
Thanks for your update with the additional information and the adapted code. Here are some updates and a new version of the code that should be helpful.
First, with this code you don’t have to set up mapping rules for your Title, ALT Text and Caption fields. That’s why I used
mla_get_data_source
in my example to get the file name (without the extension). I’ve put that back in the code below.Second, the Caption value is stored in the
post_excerpt
database field. I have added code to update that value.Third, WordPress stores the ALT Text value in a different way, in the
postmeta
table as if it was a custom field. WordPress does not store a value at all for non-image types like PDF documents. That’s why the code tests for “image metadata” and only stores an ALT Text value if the test succeeds.Finally, the
mla_mapping_updates_filter
function will not be called at all unless you register it with WordPress. You need to find a place in yourfunctions.php
file to run this statement:add_filter( 'mla_mapping_updates', 'mla_mapping_updates_filter', 10, 5 );
In the example plugin, registration is done in the
initialize
function, and there is a bit of code at the bottom of the plugin that runs theinitialize
function as WordPress loads the plugin:/* * Install the filters at an early opportunity */ add_action('init', 'MLAFileNameMappingHooksExample::initialize');
Your
functions.php
file may already have code similar to this. If not, you will have to add theinitialize
function and theadd_action
code that activates it. Be sure to changeinitialize
to something unique likemla_initialize_filter
and then change theadd_action
parameter to match it.Let me know if that makes sense, or if you still have problems/questions. Here is the updated code:
function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { /* * Derive the new Title from the file name (without the extension). * Clean up the file name for use in the Title, Caption and ALT Text fields. */ $my_setting = array( 'data_source' => 'template', 'meta_name' => '([+name_only+])', 'option' => 'raw' ); $name_only = trim( MLAOptions::mla_get_data_source( $post_id, 'single_attachment_mapping', $my_setting, NULL ) ); $new_value = str_replace( array( '-', '_', '.' ), ' ', $name_only ); /* * If $updates[ 'post_title' ], etc. is set, some mapping rule has been set up, so we respect the result. * If not, use whatever the current value is. */ if ( isset( $updates[ 'post_title' ] ) ) { $old_title = $updates[ 'post_title' ]; } else { $post = get_post( $post_id ); $old_title = $post->post_title; } /* * If the cleanup has changed the value, * put the new value in the $updates array. */ if ( $old_title != $new_value ) { $updates[ 'post_title' ] = $new_value; } if ( isset( $updates[ 'post_excerpt' ] ) ) { $old_caption = $updates[ 'post_excerpt' ]; } else { $post = get_post( $post_id ); $old_caption = $post->post_excerpt; } if ( $old_caption != $new_value ) { $updates[ 'post_excerpt' ] = $new_value; } // Only replace ALT Text if Image Metadata is present $old_metadata = get_metadata( 'post', $post_id, '_wp_attachment_metadata', true ); if ( ! empty( $old_metadata ) ) { // Find the current ALT Text value if ( isset( $updates[ 'image_alt' ] ) ) { $old_alt = $updates[ 'image_alt' ]; } else { $old_alt = get_metadata( 'post', $post_id, '_wp_attachment_image_alt', true ); } // Replace the ALT Text value with the clean file name if ( $old_alt != $new_value ) { $updates[ 'image_alt' ] = $new_value; } } return $updates; }
Thanks for the extensive answer David! really appreciate it.
I lost you at the last part with the
initialize
part.
I now have:
add_filter( 'mla_mapping_updates', 'mla_mapping_updates_filter', 10, 5 );
with next:
add_action('init', 'mla_mapping_updates_filter');
(this is possibly wrong, but I thought this is the way to connect it to the function.and the function you posted in your last reply underneath it:
function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { //left this part out for overview reasons }
all in the functions.php
is this correct?Thanks for taking the time to try and follow the instructions in my last post. The initialization process makes sense if you’re a PHP programmer, but it’s hard to describe and follow on its own.
After some further thought I realize that a better approach is to copy the entire PHP class from the example plugin along with the
add_action
code that follows it. If you just add this code to yourfunctions.php
file, replacing your previous version, it should work for you:class MLAFileNameMappingHooksExample { public static function initialize() { /* * The filters are only useful in the admin section; exit if in the "front-end" posts/pages. */ if ( ! is_admin() ) return; add_filter( 'mla_mapping_updates', 'MLAFileNameMappingHooksExample::mla_mapping_updates_filter', 10, 5 ); } /** * MLA Mapping Updates Filter * * This filter is called AFTER all mapping rules are applied. * You can add, change or remove updates for the attachment's * standard fields, taxonomies and/or custom fields. * * @since 1.00 * * @param array updates for the attachment's standard fields, taxonomies and/or custom fields * @param integer post ID to be evaluated * @param string category/scope to evaluate against: custom_field_mapping or single_attachment_mapping * @param array mapping rules * @param array attachment_metadata, default NULL * * @return array updated attachment's updates */ public static function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) { /* * Derive the new Title from the file name (without the extension). * Clean up the file name for use in the Title, Caption and ALT Text fields. */ $my_setting = array( 'data_source' => 'template', 'meta_name' => '([+name_only+])', 'option' => 'raw' ); $name_only = trim( MLAOptions::mla_get_data_source( $post_id, 'single_attachment_mapping', $my_setting, NULL ) ); $new_value = str_replace( array( '-', '_', '.' ), ' ', $name_only ); /* * If $updates[ 'post_title' ], etc. is set, some mapping rule has been set up, so we respect the result. * If not, use whatever the current value is. */ if ( isset( $updates[ 'post_title' ] ) ) { $old_title = $updates[ 'post_title' ]; } else { $post = get_post( $post_id ); $old_title = $post->post_title; } /* * If the cleanup has changed the value, * put the new value in the $updates array. */ if ( $old_title != $new_value ) { $updates[ 'post_title' ] = $new_value; } if ( isset( $updates[ 'post_excerpt' ] ) ) { $old_caption = $updates[ 'post_excerpt' ]; } else { $post = get_post( $post_id ); $old_caption = $post->post_excerpt; } if ( $old_caption != $new_value ) { $updates[ 'post_excerpt' ] = $new_value; } // Only replace ALT Text if Image Metadata is present $old_metadata = get_metadata( 'post', $post_id, '_wp_attachment_metadata', true ); if ( ! empty( $old_metadata ) ) { // Find the current ALT Text value if ( isset( $updates[ 'image_alt' ] ) ) { $old_alt = $updates[ 'image_alt' ]; } else { $old_alt = get_metadata( 'post', $post_id, '_wp_attachment_image_alt', true ); } // Replace the ALT Text value with the clean file name if ( $old_alt != $new_value ) { $updates[ 'image_alt' ] = $new_value; } } return $updates; } // mla_mapping_updates_filter } //MLAFileNameMappingHooksExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAFileNameMappingHooksExample::initialize');
The
add_action
statement at the end sets up a call to theinitialize
function, which in turn calls theadd_filter
function at an appropriate time. Putting all the code inside a PHP Class lets you use function names likeinitialize
without the risk of overwriting someone else’s code.Thanks for sticking with this evolving topic.
You sir, are awesome!
It’s awesome how much effort a plugin author can put into helping the users.
this is working, I will donate to you David! thanks!
Can I donate to you personally?Thanks for your update with the good news, and for your donation offer. Positive feedback, reviews and donations to our Fair Trade work are great motivators to keep working on the plugin and supporting its users. Fair Trade Judaica is a very personal (just my spouse and I) effort and working on the FTJ web site gave me the original idea for MLA.
- The topic ‘set title to be filename stripping characters’ is closed to new replies.