nest_author
class nest_author {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'nest-author',
__( 'Author name', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$nstauthr_auth = get_post_meta( $post->ID, 'nstauthr_auth', true );
// Set default values.
if( empty( $nstauthr_auth ) ) $nstauthr_auth = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="nstauthr_auth" class="nstauthr_auth_label">' . __( 'Author name', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="nstauthr_auth" name="nstauthr_auth" class="nstauthr_auth_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $nstauthr_auth ) . '">';
echo ' <p class="description">' . __( 'insert the name you want to display as the author', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$nstauthr_new_auth = isset( $_POST[ 'nstauthr_auth' ] ) ? sanitize_text_field( $_POST[ 'nstauthr_auth' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'nstauthr_auth', $nstauthr_new_auth );
}
}
new nest_author;