Is there any function to change the image Author(Uploaded By) in WordPress library?
2 Answers
You can use wp_update_post()
$my_post = array(
'ID' => $post_id,
'post_author' => $user_id,
);
wp_update_post( $my_post );
Or change image's author via a function using GravityForms uploader
add_action("gform_user_registered", "image_author", 10, 4);
function image_author($user_id, $config, $entry, $user_pass)
{
$post_id = $entry["post_id"];
$args = array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image'
);
$attachments = get_posts($args);
if($attachments) :
foreach ($attachments as $attachment) : setup_postdata($attachment);
$the_post = array();
$the_post['ID'] = $attachment->ID;
$the_post['post_author'] = $user_id;
wp_update_post( $the_post );
endforeach;
endif;
}
In the screenshot you provided, click on the Edit more detail
. this will redirect you to attachment's page.
Then, click on the Screen's options
on the top of screen, and check the Author
box. A new box will be added to your page.
You can there choose an author for your attachment, and save the page.
If you want to do this programmatically on upload or edit, you can use the add_attachment
or edit_attachment
filter as shown in this answer.
Otherwise, to change it manually, you can use the wp_update_post()
function. A photo is an attachment, therefore a post type, so you can change its author by passing an author to post_author
argument of this function.