Skip to content

Instantly share code, notes, and snippets.

@charlieberentsen
Created April 7, 2012 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieberentsen/2329856 to your computer and use it in GitHub Desktop.
Save charlieberentsen/2329856 to your computer and use it in GitHub Desktop.
<?php
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 ) {
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_with_dashes( $post_obj->post_title );
// random number
$random_number = rand( 10000, 99999 );
return $post_title . '-' . $random_number . $ext;
}
@franz-josef-kaiser
Copy link

I guess the add_filter call should be wrapped in an if-statement or a function that gets hooked on the needed page only using global $hook_suffix, $pagenow, $typenow or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment