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

2 Answers 2

up vote 3 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
    
I use $_GET['post'] and it does work. Take a look at the post editor screen url to determine which one is correct. Mine is like this: http://mysite.com/wp-admin/post.php?post=5&action=edit&message=1. –  Ari Susanto 2 days ago

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

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.