Question

About a year ago I remember seeing a plugin or some code which allowed you to add an author dropdown menu to the post "publishing" metabox instead of it being on its own.

I can't seem to find this code anymore and was wondering if anyone knows how to easily do this?

In an ideal situation I would like to essentially add an additional row under the "Visibility:" row within the Publish metabox stating "Author:" along with the current authors name and an edit link after the name. Upon clicking on the edit link a drop down would show up with the same information as is available in author metabox.

In any case, I would greatly appreciate to know how this can be done.

Was it helpful?

Solution

I somewhat suck with admin stuff so test this carefully. I had some issues with users not coming up in dropdown, but it fails with default meta box as well - probably because of my messed up test stack.

add_action( 'admin_menu', 'remove_author_box' );
add_action( 'post_submitbox_misc_actions', 'author_in_publish' );

function remove_author_box() {

    remove_meta_box( 'authordiv', 'post', 'normal' );
}

function author_in_publish() {

    global $post_ID;

    $post = get_post( $post_ID );
    echo '<div class="misc-pub-section">Author: ';
    post_author_meta_box( $post );
    echo '</div>';
}

OTHER TIPS

or use the plugin Adminimize for customize this via options

I know it's an old question, but an updated answer seems still relevant to give.

The original answer might have been the best option at the time but it misses checking if the post type supports changing author and fitting in the meta box style.

Main PHP

add_action('admin_menu', function (): void {
    $type = getCurrentPostType();
    if ($type) {
        remove_meta_box('authordiv', $type, 'normal');
    }
});

add_action('post_submitbox_misc_actions', function (): void {
    global $post, $user_ID;

    if (!empty($post)) {
        $supportsAuthor = post_type_supports($post->post_type, 'author');

        if ($supportsAuthor) {
            $userId = empty($post->ID) ? $user_ID : $post->post_author;
            $user = get_userdata($userId);
            echo '... fetch some template here';
        }
    }
});

Helper PHP

function getCurrentPostType(): ?string {
    // https://gist.github.com/bradvin/1980309
    global $post, $typenow, $current_screen;

    if ($post && $post->post_type) {
        return $post->post_type;
    } elseif ($typenow) {
        return $typenow;
    } elseif ($current_screen && $current_screen->post_type) {
        return $current_screen->post_type;
    } elseif (isset($_REQUEST['post_type'])) {
        return sanitize_key($_REQUEST['post_type']);
    } elseif (isset($_REQUEST['post'])) {
        $p = get_post($_REQUEST['post']);
        if ($p) {
            return $p->post_type;
        }
    }

    // Unknown
    return null;
}

PHTML

<div class="misc-pub-section misc-pub-author" id="author">
    <?=sprintf(__('Author: %s', 'aym'), "<b>{$user->display_name}</b>")?>
    <a href="#author" class="edit-author hide-if-no-js" role="button" aria-label='<?=__('Edit author', 'aym')?>'>
        <?=__('Edit', 'aym')?>
    </a>

    <div id="post-author-select" class="hide-if-js" style="display: none;">
        <?php post_author_meta_box($post)?>

        <p>
            <a href="#author" class="save-post-author hide-if-no-js button">OK</a>
            <a href="#author" class="cancel-post-author hide-if-no-js button-cancel">Cancel</a>
        </p>
    </div>
</div>

JavaScript

(function($) {
    // Author select in admin edit sidebar
    var $baseEl = $('#author');
    var $select = $('select', $baseEl);
    var $form   = $('#post-author-select', $baseEl);
    var previousValue = $select.val();
    $('a[href="#author"]', $baseEl).on('click', function(e) {
        e.preventDefault();
        $form.slideToggle(300);
    });

    $('.save-post-author', $baseEl).on('click', function(e) {
        e.preventDefault();
        previousValue = $select.val();
        $('b', $baseEl).text($("option:selected", $select).text());
        $form.slideUp(300);
    });

    $('.cancel-post-author', $baseEl).on('click', function(e) {
        e.preventDefault();
        $select.val(previousValue);
        $form.slideUp(300);
    });
})(jQuery);

CSS

.misc-pub-author::before {
    content: "\f110";
    color: #82878c;
    font: normal 20px/1 dashicons;
    speak: none;
    display: inline-block;
    margin-left: -1px;
    padding-right: 3px;
    vertical-align: top;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#post-author-select {
    padding-top: 5px;
}

#post-author-select select {
    width: 100%;
}

p.s. If you encounter errors it could be because the original version I use is made in SCSS, TypeScript and PHP 7.2. And uses a custom template engine.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top