Limited Support
Our team is currently on holiday, so support will be limited during this period. Response times may be slower than usual, and some inquiries may be delayed.
We appreciate your patience and understanding, and we’ll resume our usual support by the end of August.
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 );
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 ) );