Co-Authors-Plus icon indicating copy to clipboard operation
Co-Authors-Plus copied to clipboard

Possible to start a new post with no selected author

Open jonathanstegall opened this issue 2 years ago • 2 comments

When creating a new post, the list of authors defaults to the current logged in user. I realize why it does this, since wp_posts requires a value for the post_author. I'm wondering if it would be possible to only set the value to the logged in user on the first post save, similarly to how a post is assigned to Uncategorized if it doesn't have any categories.

I'm asking because most of the people who create posts in our WordPress are editors, they're rarely the authors, so the have to both remove themselves as the author and then assign the author(s). It seems like it would be a nice option to have a blank value at first.

jonathanstegall avatar Jul 19 '22 18:07 jonathanstegall

As a test, I tried running this combination of filters, but I think this criteria prevents it from running. It does cause a PHP error because default author is not an object, but it doesn't result in a blank meta field.

add_filter( 'coauthors_default_author', 'set_default_author', 10, 1 );
function set_default_author( $author_id ) {
	$author_id = 0;
	return $author_id;
}

add_filter( 'coauthors_guest_authors_force', 'coauthors_guest_authors_force', 10, 1 );
function coauthors_guest_authors_force( $force_guest_authors ) {
	$force_guest_authors = true;
	return $force_guest_authors;
}

jonathanstegall avatar Jul 19 '22 18:07 jonathanstegall

One way that does appear to work is to change the coauthors_meta_box method after it runs the already existing default user filter. I'm not sure what implications this might have, but would be happy to submit a pull request if it would be helpful.

$default_user = apply_filters( 'coauthors_default_author', wp_get_current_user() );

if ( '' !== $default_user ) {
    // @daniel, $post_id and $post->post_author are always set when a new post is created due to auto draft,
    // and the else case below was always able to properly assign users based on wp_posts.post_author,
    // but that's not possible with force_guest_authors = true.
    if ( ! $post_id || 0 === $post_id || ( ! $post->post_author && ! $coauthors_plus->force_guest_authors ) || ( 'post' === $current_screen->base && 'add' === $current_screen->action ) ) {
        $coauthors = array();
        // If guest authors is enabled, try to find a guest author attached to this user ID
        if ( $this->is_guest_authors_enabled() ) {
            $coauthor = $coauthors_plus->guest_authors->get_guest_author_by( 'linked_account', $default_user->user_login );
            if ( $coauthor ) {
                $coauthors[] = $coauthor;
            }
        }
        // If the above block was skipped, or if it failed to find a guest author, use the current
        // logged in user, so long as force_guest_authors is false. If force_guest_authors = true, we are
        // OK with having an empty authoring box.
        if ( ! $coauthors_plus->force_guest_authors && empty( $coauthors ) ) {
            if ( is_array( $default_user ) ) {
                $coauthors = $default_user;
            } else {
                $coauthors[] = $default_user;
            }
        }
    } else {
        $coauthors = get_coauthors();
    }
} else {
    $coauthors = array();
}

jonathanstegall avatar Jul 19 '22 18:07 jonathanstegall