Support » Plugin: Media File Renamer: Rename for better SEO (AI-Powered) » Cannot rename filenames with empty post title field using filter

  • Hi!

    First of all, thanks for a great plugin. This is my second time using it and it works great (besides encoding issues =P).

    I wanted to rename filenames that contained accents ONLY. So i used a filter of yours to target these filenames (thank you for giving us this opportunity) which works great.

    But! I have some filenames where the post titles are empty and because of this, these filenames cannot be renamed. I made it work by changing two lines in the code so that we check both post title and the custom filename (from the filter) if they are empty.

    I don’t know if this is a feature you would like but it helped in my situation where a filename existed, but just not in the post title field.

    Let me know if you think this is something you’d be willing to implement in the core.

    / Panos

    https://wordpress.org/plugins/media-file-renamer/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Hello Panos,

    Good idea, I have never thought about that since I basically only rename automatically from the title. Would you mind sharing the portion of code you changed here? I will test it a bit (though I think I know what you did) and will apply it to my code 🙂

    Thread Starter PHPanos

    (@phpanos)

    Hi Jordy,

    Basically I just moved the lines 989 and 990 in media-file-renamer.php:

    $sanitized_media_title = $this->new_filename( $post, $base_new_title, $forceFilename );
      $this->log( "New file should be: " . $sanitized_media_title );

    to right above this code on line 971-972:

    // Empty post title when renaming using title? Let's not go further.
      if ( !$force && empty( $base_new_title ) ) {

    and also edited the if statement above to:
    if ( !$force && empty( $base_new_title ) && empty( $sanitized_media_title ) ) {

    This was really just to fit my needs and could probably need a bit of testing, but the main issue is just the check on line 972 (when using the “mfrh_new_filename” filter) 🙂

    Thread Starter PHPanos

    (@phpanos)

    And if this “fix” would be implemented, you probably want to adjust the check for empty post_title in the generate_explenation function to avoid the message “This title cannot be used for a filename” when a title has been generated through the filter instead of the post_title field. And probably just have a general title variable regardless of where the title comes from, whether it’s from the post_title field or a filter.

    Hope this makes sense.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Cannot rename filenames with empty post title field using filter’ is closed to new replies.