Jun 16, 2023 7:31 am
Hi,
I would like to rename the attached files of a comment (using the media uploader plugin) and use the title of the current post. So I created a snippet to add this code when the sanitize_file_name function is call.
But this doesn't work, it seems that I can't acces to the post, even if I use a global $post.
Do you have an idea ?
Thanks
Â
function my_custom_file_name( $filename ) { $info = pathinfo( $filename ); $ext = empty( $info['extension'] ) ? '' : '.' . $info['extension']; $name = basename( $filename, $ext ); if ( isset( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) { $postObj = get_post( $_REQUEST['post_id'] ); $postSlug = sanitize_title( $postObj->post_title ); } if ( isset( $postSlug ) && ! empty( $postSlug ) ) { $finalFileName = $postSlug; } // File name will be the same as the post slug. else { $finalFileName = sanitize_title( $name ); } // File name will be the same as the image file name, but sanitized. return $finalFileName . $ext; } add_filter( 'sanitize_file_name', 'my_custom_file_name', 100 );
4 Replies
Jun 16, 2023 1:05 pm
New test : when I try to get the url of my page, I only get the base url of my domain, not the full url of my post...
Is it because the upload is done in js ?
global $wp; $current_url = home_url( add_query_arg( array(), $wp->request ) );