Add Author Details to Edit Post Screen in WordPress

A few days ago, I received a mail from one of my users. She wanted to know how to add the author details in a edit post screen ( admin side ).

Adding anything to the admin side ( edit post ) requires the use of a function add_meta_box.

This function should be called from inside a callback on the hook add_meta_boxes.

add_action( 'add_meta_boxes', 'callback_add_meta_boxes' );
function callback_add_meta_boxes() {
     add_meta_box(
        'metabox_id',
        __( 'Metabox Title', 'text-domain' ),
        'metabox_callback',
        'post',
        'normal',
        'low',
        array('arg1' => "some value', 'arg2' => 'some other value' )
    );
}

This code will create a new meta box in the edit screen of any post.

Here callback_add_meta_boxes is the callback function and the name is arbitrary.

The $metabox_id is the id of the new meta box. The metabox_callback is the function that will generate the HTML for the box.

The post parameter indicates that this box will be added for any post.

The normal parameter is the value for context of this box within the screen which can be normal, aside or advanced.

The last array is the second argument for the callback function and takes any arbitrary number of values that you want.

The callback function is responsible for generating the display HTML. It receives the $post as its first parameter. Let us create the function –

function metabox_callback( $post, $args) {
    
    $author_id = $post->author;
    $author_fname = get_the_author_meta( 'first_name', $author_id );
    $author_lname = get_the_author_meta( 'last_name', $author_id );
    $email = get_the_author_meta( 'email', $author_id );
    $description = get_the_author_meta( 'description', $author_id );
    $author_name = $author_fname . " " . $author_lname;
    ?>
     
     <div>
        <p>
            <b>Name</b>: <?php echo $author_name; ?>
        </p>
        <p>
            <b>Email</b>: <?php echo $email; ?>
        </p>
        <p>
            <b>Description</b>: <?php echo $description; ?>
        </p>
     </div>
    
    <?php
}

It will generate an Author Details box in the edit post screen (on the right) as shown below –

Let us understand how the function works –

    $author_id = $post->author;
    $author_fname = get_the_author_meta( 'first_name', $author_id );
    $author_lname = get_the_author_meta( 'last_name', $author_id );
    $email = get_the_author_meta( 'email', $author_id );
    $description = get_the_author_meta( 'description', $author_id );
    $author_name = $author_fname . " " . $author_lname;

The function receives the $post value. So we are getting the ID of the author using $post->author.

The the details of the author is extracted with the help of the get_the_author_meta function.

And lastly, the same details are echoed out into the screen.

The important thing to note here is that the Author must already have values for the fields it is displaying.

Secondly, this display function metabox_callback can be used to display any conceivable value, not only the post author and can also be used on a page edit screen by changing the parameter post to page in callback function callback_add_meta_boxes.