Coauthors
class Brief_Sensei_Coauthors {
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(
'coauthor',
__( 'Coauthors', 'brief_sensei_coauthors' ),
array( $this, 'render_metabox' ),
'course',
'side',
'default'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'brief_nonce_action', 'brief_nonce' );
// Retrieve an existing value from the database.
$brief_brief_sensei_coathor = get_post_meta( $post->ID, 'brief_brief_sensei_coathor', true );
// Set default values.
if( empty( $brief_brief_sensei_coathor ) ) $brief_brief_sensei_coathor = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="brief_brief_sensei_coathor" class="brief_brief_sensei_coathor_label">' . __( 'Coauthors', 'brief_sensei_coauthors' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="brief_brief_sensei_coathor" name="brief_brief_sensei_coathor" class="brief_brief_sensei_coathor_field" placeholder="' . esc_attr__( '', 'brief_sensei_coauthors' ) . '">' . $brief_brief_sensei_coathor . '</textarea>';
echo ' <p class="description">' . __( 'Add extra authors besides teacher', 'brief_sensei_coauthors' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['brief_nonce'] ) ? $_POST['brief_nonce'] : '';
$nonce_action = 'brief_nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$brief_new_brief_sensei_coathor = isset( $_POST[ 'brief_brief_sensei_coathor' ] ) ? sanitize_text_field( $_POST[ 'brief_brief_sensei_coathor' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'brief_brief_sensei_coathor', $brief_new_brief_sensei_coathor );
}
}
new Brief_Sensei_Coauthors;