Last active
December 16, 2015 09:48
-
-
Save codee47/5415161 to your computer and use it in GitHub Desktop.
Rename the uploaded media file to post title.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*-----------------------------------------------------------------------------------*/ | |
/* Rename the Uploaded Media File to Post Title | |
/*-----------------------------------------------------------------------------------*/ | |
add_filter( 'sanitize_file_name', 'cb_sanitize_file_name' ); | |
/** | |
* @internal Missing short description | |
* | |
* @link wp-includes|formatting.php | |
* @todo causing errors at media-new.php | |
*/ | |
function cb_sanitize_file_name( $filename ) { | |
global $post; | |
if ( isset( $_REQUEST['post_id'] ) ) | |
$post_id = $_REQUEST['post_id']; | |
else | |
$post_id = false; | |
// fetch the file extension | |
$info = pathinfo( $filename ); | |
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension']; | |
// get the post object | |
$post_obj = get_post( $post_id ); | |
// get the post title | |
$post_title = sanitize_title( $post_obj->post_title ); | |
// random number | |
$random_number = rand( 10000, 99999 ); | |
return $post_title . '-' . $random_number . $ext; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment