Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can i get "post id" in post edit area for add_filter in functions.php?

I want to change file name automatically during upload. so i need to obtain post id.

the code is as follows:

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name' );   
function custom_upload_name($file)
{       
  list($name, $ext) = explode('.', $file['name']);
  $file['name'] = $post_ID.'.'.$ext;    
return $file;
}

Edited:

the final and working code:

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name' );   
function custom_upload_name($file)
{       
  $post_ID = $_POST['post_id'];   
  list($name, $ext) = explode('.', $file['name']);
  $file['name'] = $post_ID.'.'.$ext;    
return $file;
}
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Try using $_POST['post_id'] in the upload area as file.php does not include the global $post object.

share|improve this answer
    
$post_ID = $_GET['post_id'] dose not work. –  kami Jul 24 '12 at 16:35
    
@kami Updated my answer. Use $_POST instead. –  Brian Fegter Jul 24 '12 at 16:44
    
thanks a lot, u saved me! –  kami Jul 24 '12 at 17:01
add comment

Did you tried using global $post object?

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name' );   

function custom_upload_name($file){ 
  global $post;      
  list($name, $ext) = explode('.', $file['name']);
  $file['name'] = $post->ID.'.'.$ext;    
return $file;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.