Menu Adam R Brown

WP hooks navigation: Home/browseActions indexFilters index

Source View: wp_unique_filename

To save our bandwidth, we show only a snippet of code around each occurence of the hook. View complete file in SVN (without highlighting).

Understanding Source Code

The best way to understand what a hook does is to look at where it occurs in the source code.

Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.

Source View

This hook occurs 2 times in this file.

Line Code
2292                 * Filters the result when generating a unique file name.
2293                 *
2294                 * @since 4.5.0
2295                 *
2296                 * @param string        $filename                 Unique file name.
2297                 * @param string        $ext                      File extension, eg. ".png".
2298                 * @param string        $dir                      Directory path.
2299                 * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
2300                 */
2301                return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
2302           }
2303
2304           while ( file_exists( $dir . "/$filename" ) ) {
2305                $new_number = (int) $number + 1;
2306                if ( '' == "$number$ext" ) {
2307                     $filename = "$filename-" . $new_number;
2308                } else {
2309                     $filename = str_replace( array( "-$number$ext", "$number$ext" ), '-' . $new_number . $ext, $filename );
2310                }
2311                $number = $new_number;
2312           }
2313      }
2314
2315      /** This filter is documented in wp-includes/functions.php */
2316      return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
2317 }
2318
2319 /**
2320  * Create a file in the upload folder with given content.
2321  *
2322  * If there is an error, then the key 'error' will exist with the error message.
2323  * If success, then the key 'file' will have the unique file path, the 'url' key
2324  * will have the link to the new file. and the 'error' key will be set to false.
2325  *