Leave a Reply

2 comments

  1. Use this in your theme’s functions.php:

    add_filter( 'add_attachment', 'wpse_55801_attachment_author' );
    
    function wpse_55801_attachment_author( $attachment_ID ) 
    {
        $attach = get_post( $attachment_ID );
        $parent = get_post( $attach->post_parent );
    
        $the_post = array();
        $the_post['ID'] = $attachment_ID;
        $the_post['post_author'] = $parent->post_author;
    
        wp_update_post( $the_post );
    }
    
  2. I extended the above solution of @brasofilo to also change the attachment post date to the date of the post parent.

    And not only on upload of the attachment, but also when an attachment is edited. You can use the edit_attachment filter with the same function for this. However, when you do that, the wp_update_post function causes an infinite loop and leads to PHP memory errors, I found out the hard way after a lot of searching.
    A warning for this infinite loop is actually mentioned in the Codex.

    A solution is to remove the filters, like so:

    function wpse_55801_attachment_author( $attachment_ID ) {
    
        remove_filter('add_attachment', 'wpse_55801_attachment_author');
        remove_filter('edit_attachment', 'wpse_55801_attachment_author');
    
        $attach = get_post( $attachment_ID );
        $parent = get_post( $attach->post_parent );
    
        $the_post = array();
        $the_post['ID'] = $attachment_ID;
        $the_post['post_author'] = $parent->post_author;
        $the_post['post_date'] = $parent->post_date;
    
        wp_update_post( $the_post );
    
        add_filter( 'add_attachment', 'wpse_55801_attachment_author' );
        add_filter( 'edit_attachment', 'wpse_55801_attachment_author' );
    }   
    
    add_filter( 'add_attachment', 'wpse_55801_attachment_author' );
    add_filter( 'edit_attachment', 'wpse_55801_attachment_author' );