1

For security reasons, I want to make sure no content is attributed to admin users of my websites.

Essentially, I want a way to automatically change the author of any piece of content to something else (including uploads).

As an admin, when I delete a user, WordPress asks me if I want to delete all their content or reattribute it to another user.

Well, I want to automatically change the author of content from a given user (an admin user) to another user (an author/editor).

How can I accomplish this (I generally have root level access to the servers hosting the sites)?

2

Hook into a post status transition so that any time a post is published or updated, there's a check to see if the author is an admin. If so, update the author to a non-admin. First, get the ID of the non-admin user you want to set as the author.

// change author ID to suit your needs
$newAuthor = 3;
// hook our function to fire when any post type's status changes
add_action('transition_post_status', 'wpse_change_author', 10, 3);
function wpse_change_author($new_status, $old_status, $post) {
    // only adjust published posts - which are either new or newly updated
    if($new_status == 'publish') {
        // check whether author is admin
        if(user_can($post->post_author, 'administrator')) {
            // update the post with new author ID
            wp_update_post(array('ID' => $post->ID, 'post_author' => $newAuthor));
        }
    }
}
1
  • Thank you, I haven't implemented/tested this yet but it sounds like it will do exactly what I want so I marked this answer as accepted. – adam-asdf Oct 13 '17 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.