Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridhamdholakia/a082f8c67fde1a24f47c6bee87518ce4 to your computer and use it in GitHub Desktop.
Save ridhamdholakia/a082f8c67fde1a24f47c6bee87518ce4 to your computer and use it in GitHub Desktop.
/*============================= Custom Meta Box (Assign Articles) To Custom Post Type ============================ */
function assign_article_metabox() {
$post_types = array( 'your-post-type' ); // you can use multiple post types here(array of post types)
foreach ( $post_types as $post_type ) {
add_meta_box(
'member_sectionid',
__( 'Name Of Metabox', 'member_textdomain' ),
'assign_article_metafields',
$post_type
);
}
}
add_action( 'add_meta_boxes', 'assign_article_metabox' );
function assign_article_metafields( $post ) {
wp_nonce_field( 'assign_articles_savedata', 'article_meta_box_nonce' );
$editor = explode( ',', get_post_meta( $post->ID, '_engaged_editors', true ) );
$peerviewer = explode( ',', get_post_meta( $post->ID, '_engaged_peerviewers', true ) );?>
<table class="form-table fh-profile-upload-options">
<tr>
<th><label for="twitter">Editors</label></th>
<th><label for="twitter">Peer Viewers</label></th>
</tr>
<tr>
<td>
<select name="editors[]" id="editor" class="regular-text form-control-chosen" data-placeholder="Please select editors..." multiple>
<option>-- Select Editors --</option>
<?php
$editors = array(
'role' => 'editor',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users_editors = get_users( $editors );
foreach( $users_editors as $user ): ?>
<option value="<?php echo $user->ID; ?>" <?php if(in_array($user->ID, $editor)) { echo "selected"; } ?>><?php echo esc_html( $user->display_name ); ?></option>
<?php endforeach; ?>
</select>
<br/>
<span class="description">Please Select Editors.</span>
</td>
<td>
<select name="peerviewers[]" id="editor_category" class="regular-text form-control-chosen" data-placeholder="Please select peerviewers..." multiple>
<option>-- Select Peerviewers --</option>
<?php
$peerviewers = array(
'role' => 'peerviewer',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users_peerviewers = get_users( $peerviewers );
foreach( $users_peerviewers as $user ): ?>
<option value="<?php echo $user->ID; ?>" <?php if(in_array($user->ID, $peerviewer)) { echo "selected"; } ?>><?php echo esc_html( $user->display_name ); ?></option>
<?php endforeach; ?>
</select>
<br/>
<span class="description">Please Select Peerviewers.</span>
</td>
</tr>
</table>
<?php
}
function assign_articles_savedata( $post_id ) {
if ( ! isset( $_POST['article_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['article_meta_box_nonce'], 'assign_articles_savedata' ) ) {
return;
}
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;
}
}
if ( ! isset( $_POST['editors'] ) ) {
return;
}
if ( ! isset( $_POST['peerviewers'] ) ) {
return;
}
$editors = implode(',', $_POST['editors'] );
$peerviewers = implode(',', $_POST['peerviewers'] );
update_post_meta( $post_id, '_engaged_editors', $editors );
update_post_meta( $post_id, '_editor_edited', '0' );
update_post_meta( $post_id, '_engaged_peerviewers', $peerviewers );
}
add_action( 'save_post', 'assign_articles_savedata' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment