• I am tasked with painstakingly renaming 600+ images on our site with SEO engorged alt text, titles, and filenames. Because of this, I find myself using the Attachment Details view since I can just use the left and right arrows to navigate.

    Given this, is it possible to add the filename input dialogue onto the attachment details view? I have been playing around with your plugin and some custom scripting to try and get the functionality but I just cant get it to work.

    Creating a custom script to add that field to the attachment details view as separate metadata and then calling do_rename() on it just crashes my whole installation for some reason.

    If this isn’t something you plan to include in the plugin, is this something I can achieve using a code snippet plugin?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author crossi72

    (@crossi72)

    Hi @tonytable,
    I never added the rename function to the Attachment Detail view because it is triky: in the list view and in the attachement page there are action and filter to add custom fields and manage file update, I can check in now something similar has been added also in Attachment Detail.

    Did you find some action or filter that can be used to add the filename textbox and to start the renaming process? If so, I can try to add the renaming function even in Attachment Detail view.

    C.

    Thread Starter tonytable

    (@tonytable)

    Yes, specifically using this snippet of code in a plugin that appends functions.php I can add an entry to the attachment details page:

    //function to add custom media field
    function custom_media_add_media_custom_field( $form_fields, $post ) {
        $field_value = get_post_meta( $post->ID, 'edit_filename', true );
    
        $form_fields['edit_filename'] = array(
            'value' => $field_value ? $field_value : '',
            'label' => __( 'Filename' ),
            'helps' => __( 'Enter new filename' ),
            'input'  => 'input'
        );
    
        return $form_fields;
    } 
    
    //add metadata field to attachment page
    add_filter( 'attachment_fields_to_edit', 'custom_media_add_media_custom_field', null, 2 );

    What I had tried then after that was to run your code when the edit attachment action runs:

    //save your custom media field
    function custom_media_save_attachment( $attachment_id ) {
        if ( isset( $_REQUEST['attachments'][ $attachment_id ]['edit_filename'] ) ) {
            
            //Get value stored in attachment details form
            $edit_filename = $_REQUEST['attachments'][ $attachment_id ]['edit_filename'];
            
            //Save filename to metadata
            update_post_meta( $attachment_id, 'edit_filename', $edit_filename );
    
            //Run rename using phoenix plugin
            Phoenix_Media_Rename::do_rename($attachment_id, $edit_filename);
        }
    }
    add_action( 'edit_attachment', 'custom_media_save_attachment' );

    But doing this essentially crashes the server for a while (my database kept crashing because it was saying it was receiving too many calls, so I had to wait about 30-45 minutes for that to fix itself.)

    I did find out though, that in your add_filename_field function, if I remove the stipulations of it being a post and attachment, the filename field does show up on the attachment details page.

    /**
     * Add the "Filename" field to the Media form
     *
     * @param [type] $form_fields
     * @param [type] $post
     * @return array form fields
     */
    function add_filename_field($form_fields, $post) {
       //if (isset($GLOBALS['post']) && $GLOBALS['post']->post_type=='attachment') {
          $file_parts=$this->get_file_parts($GLOBALS['post']->ID);
          $form_fields['mr_filename']=array(
             'label' => __('Filename', 'phoenix-media-rename'),
    	 'input' => 'html',
             'html' => $this->get_filename_field($GLOBALS['post']->ID, $file_parts['filename'], $file_parts['extension'])
          );
       //}
       return $form_fields;
    }

    That is about all I found out before I decided to reach out to you.

    • This reply was modified 3 years, 10 months ago by tonytable.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Edit filename in attachement details?’ is closed to new replies.