Make WordPress Core


Ignore:
Timestamp:
06/20/2024 04:02:10 PM (2 years ago)
Author:
adamsilverstein
Message:

Media: improve titles when inserting via REST API.

Match the naming behavior for uploaded media in the REST API to the way media is named when uploading in the media library. Fix an issue where dashes were replacing spaces unnecessarily.

Props abitofmind, kadamwhite, spacedmonkey, adamsilverstein, audrasjb, hellofromTonya.
Fixes #57957.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r58130 r58447  
    305305                $attachment->guid           = $url;
    306306
     307                // If the title was not set, use the original filename.
     308                if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
     309                        // Remove the file extension (after the last `.`)
     310                        $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );
     311
     312                        if ( ! empty( $tmp_title ) ) {
     313                                $attachment->post_title = $tmp_title;
     314                        }
     315                }
     316
     317                // Fall back to the original approach.
    307318                if ( empty( $attachment->post_title ) ) {
    308319                        $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
Note: See TracChangeset for help on using the changeset viewer.