The effects of alcohol on sustanon 250 leucine for – real weight loss & bodybuilding benefits?
Search
Close
AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

AI Assistant
Rename a file durin...
 
Notifications
Clear all

Question [Solved] Rename a file during upload with the post title

5 Posts
2 Users
2 Reactions
1,227 Views
 Benn
(@benn)
Posts: 3
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 
[#7428]

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 );

 
Posted : 16/06/2023 11:31 am
 Benn
(@benn)
Posts: 3
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

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 ) );

 
Posted : 16/06/2023 5:05 pm
Asti
 Asti
(@asti)
Posts: 8259
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@benn,

The wp_insert_attachment function is used for inserting the attachments. The wp_insert_attachment calls the wp_insert_post function as well. So you should use those functions. 

As far as the file uploading use the AJAX method you can't change the filename using the sanitize_file_name. The global post isn't accessible when the AJAX method is used for file uploading. 


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 19/06/2023 12:08 pm
Benn reacted
 Benn
(@benn)
Posts: 3
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@asti Thank you for your answer and your explanation !

I finally use the add_attachment function and it works well !
Here is my code, if it could helps somebody.

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

    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename

    $post_title = get_the_title(get_post_parent($post_ID));
    $user = wp_get_current_user()->display_name;
    $newfilename = $user.'-'.$post_title;
    $newfile = $path['dirname'].'/myFolder/'.$newfilename.'.'.$path['extension'];

     // Update Wordpress 
     rename($file, $newfile);    
     update_attached_file( $post_ID, $newfile );

    // Update metadata
    $pathNewFile = pathinfo($newfile);
    $meta = wp_get_attachment_metadata($post_ID);
    $meta['file'] = str_replace( $path['basename'], $pathNewFile['basename'], $meta['file'] );
    wp_update_attachment_metadata( $post_ID, $meta );

    // Update media library post title
    $post = get_post($post_ID); 
    $post->post_title = $newfilename;
    wp_update_post( $post );


}

 
Posted : 20/06/2023 1:34 am
Asti reacted
Asti
 Asti
(@asti)
Posts: 8259
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

Thank you for sharing the information. We really appreciate it.

We're going to mark this topic as solved and close it.

Feel free to open a new topic if you have any other questions.


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 20/06/2023 11:13 am