6 replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
source | link

I know there a some functions already avaible here for this task, but none of them seem to work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiserkaiser does in this examplethis example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like IjaasIjaas does herehere. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

I know there a some functions already avaible here for this task, but none of them seem to work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

I know there a some functions already avaible here for this task, but none of them seem to work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

5 corrected spelling
source | link

I know there a some functions already avaible here for this task, but none of them seme woseem to work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

I know there a some functions already avaible here for this task, but none of them seme wo work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

I know there a some functions already avaible here for this task, but none of them seem to work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

    Tweeted twitter.com/#!/StackWordPress/status/315142549629181952
4 added the endless loop function
source | link

I know there a some functions already avaible here for this task, but none of them seme wo work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

I know there a some functions already avaible here for this task, but none of them seme wo work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

I know there a some functions already avaible here for this task, but none of them seme wo work as needed.

The best way seems to me adding a filter to wp_handle_upload_prefilter like kaiser does in this example. Unfortunately the file only gets the parent-post-title if the post is already saved to the database.


Another approach is to add a function to add_attachment like Ijaas does here. Then the file gets the parent-post-title as name, but no thumbnails are created. And my attempt in using wp_generate_attachment_metadata(); to create the missing image sizes, ended in an endless loop (propably cause I'm using it the wrong way, but now I'm a bit scared by this function).


Would be great if there was a way to pass the title to wp_handle_upload_prefilter even if the post isn't saved yet.

Oh by the way, this is my endless function, maybe somebody can tell me what's wrong with it. DON'T USE THIS FUNCTION !!!

add_action('add_attachment', 'fkp_rename_attacment');
function fkp_rename_attacment($post_ID){

  $post = get_post($post_ID);
  $file = get_attached_file($post_ID);
  $path = pathinfo($file);
  $parent = get_post($post->post_parent);
  $p_author = get_the_author_meta( 'display_name', $parent->post_author );
  $p_author_san = sanitize_title($p_author);

  $newfilename = $parent->post_name . '-' . $p_author_san . '-' . $post_ID;
  $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

  rename($file, $newfile);    

  $wp_filetype = wp_check_filetype(basename($newfile), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $newfile ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($newfile)),
    'post_content' => '',
    'post_status' => 'inherit'
   );
  $attach_id = wp_insert_attachment( $attachment, $newfile, $parent->ID );
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  $attach_data = wp_generate_attachment_metadata( $attach_id, $newfile );
  wp_update_attachment_metadata( $attach_id, $attach_data );
}

DON'T USE THIS FUNCTION !!!

3 added 87 characters in body
source | link
2 deleted 28 characters in body
source | link
1
source | link