Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress module to add and display a guest author's name and title
<?php
/**
*
* Add ability to add guest authors
*/
function myblog_guest_author_name( $original_name ) {
if (in_the_loop()) {
$guest_name = get_post_meta( get_the_ID(), 'guest_author_name', true );
return ( $guest_name ) ? $guest_name : $original_name;
} else {
return $original_name;
}
}
add_filter( 'the_author', 'myblog_guest_author_name' );
add_filter( 'get_the_author_display_name', 'myblog_guest_author_name' );
function myblog_guest_author_meta_box( $post_type, $post ) {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'guest_author_meta_box',
__( 'myblog Guest Author','myblog' ),
'myblog_render_guest_author_meta_box',
$screen,
'normal',
'default'
);
}
}
add_action( 'add_meta_boxes', 'myblog_guest_author_meta_box', 10, 2 );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myblog_render_guest_author_meta_box( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'myblog_guest_author_meta_box', 'myblog_guest_author_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$name_value = get_post_meta( $post->ID, 'guest_author_name', true );
echo '<label for="guest_author_name">';
_e( 'Name', 'myblog' );
echo '</label> ';
echo '<input type="text" id="guest_author_name" name="guest_author_name" value="' . esc_attr( $name_value ) . '" size="25" />';
echo "<br>";
$title_value = get_post_meta( $post->ID, 'guest_author_title', true );
echo '<label for="guest_author_title">';
_e( 'Title', 'myblog' );
echo '</label> ';
echo '<input type="text" id="guest_author_title" name="guest_author_title" value="' . esc_attr( $title_value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myblog_save_guest_author_meta_box( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myblog_guest_author_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myblog_guest_author_meta_box_nonce'], 'myblog_guest_author_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set and update the meta field in the database.
if ( isset($_POST['guest_author_name'])) {
update_post_meta( $post_id, 'guest_author_name', sanitize_text_field($_POST['guest_author_name']) );
}
if (isset($_POST['guest_author_title'])) {
update_post_meta( $post_id, 'guest_author_title', sanitize_text_field($_POST['guest_author_title']) );
}
}
add_action( 'save_post', 'myblog_save_guest_author_meta_box' );
/**
* Prints HTML with meta information for the current post author.
*/
function myblog_posted_by() {
global $post;
echo '<div class="author-info">';
// Don't link up guest author names
if (get_post_meta( $post->ID, 'guest_author_name', true ) != '') {
$name_html = esc_html( get_the_author() );
} else {
$name_html = '<a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a>';
}
$byline = sprintf(
_x( 'by %s', 'post author', 'myblog' ),
'<span class="author vcard">' . $name_html . '</span>'
);
// Display gravatar
echo get_avatar( get_the_author_meta( 'ID' ) , 34 );
echo '<div class="meta">';
echo '<span class="byline"> ' . $byline . '</span>';
// Display (job) title
$guest_title = get_post_meta( $post->ID, 'guest_author_title', true );
$title = ($guest_title != '') ? $guest_title : get_the_author_meta( "job-title" );
echo '<span class="jobtitle">' . $title .'</span>';
echo "</div>";
echo "</div>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment