[Wordpress] Add metabox and custom column
<?php
/**
* Custom metabox to change priority displaying courses
* ====================================================
*/
add_action( 'add_meta_boxes', 'tj_amb_priority_courses' );
function tj_amb_priority_courses() {
add_meta_box( 'tj_mb_priority_courses', 'Display Priority', 'tj_mb_priority_courses_cb', 'sfwd-courses', 'side', 'high' );
}
function tj_mb_priority_courses_cb($post) {
wp_nonce_field( 'tj_mb_priority_courses_nonce', 'mb_priority_courses_nonce' );
$val = get_post_meta( $post->ID, 'sfwd-display-priority', true );
?>
<div class="sfwd_input" id="sfwd-display-priority">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!" onclick="toggleVisibility('sfwd-display_priority_tip');">
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/question.png">
<label class="sfwd_label textinput">
Display Priority
</label>
</a>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<input name="sfwd-display-priority" type="number" value="<?php echo $val; ?>" style="width:70%;">
</div>
<div class="sfwd_help_text_div" style="display:none" id="sfwd-display_priority_tip">
<label class="sfwd_help_text">
Enter the ordered number to be displayed.<br>
Smaller value will appear at the top.
</label>
</div>
</span>
<p style="clear:left">
</p>
</div>
<?php
}
add_action( 'save_post', 'tj_mb_save_priority_courses' );
function tj_mb_save_priority_courses( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['mb_priority_courses_nonce'], 'tj_mb_priority_courses_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
// Make sure your data is set before trying to save it
if( isset( $_POST['sfwd-display-priority'] ) )
update_post_meta( $post_id, 'sfwd-display-priority', $_POST['sfwd-display-priority'] );
}
/**
* Add priority to column on view all course
*/
add_filter( 'manage_edit-sfwd-courses_columns', 'tj_sfwd_courses_columns' ) ;
function tj_sfwd_courses_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title' ),
'categories' => __( 'Categories' ),
'tags' => __( 'Tags' ),
'comments' => __( 'Comments' ),
'priority' => __( 'Priority' ),
'date' => __( 'Date' ),
);
return $columns;
}
add_action( 'manage_sfwd-courses_posts_custom_column', 'tj_manage_sfwd_courses_columns', 10, 2 );
function tj_manage_sfwd_courses_columns( $column, $post_id ) {
global $post;
switch( $column ) {
/* If displaying the 'priority' column. */
case 'priority' :
/* Get the priority for the post. */
$terms = get_post_meta( $post_id, 'sfwd-display-priority', true );
/* If terms were found. */
if ( !empty( $terms ) ) {
echo $terms;
}
/* If no terms were found, output a default message. */
else {
_e( 'No Priority' );
}
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
/* Enable sortable for priority column */
add_filter( 'manage_edit-sfwd-courses_sortable_columns', 'tj_sfwd_courses_sortable_columns' );
function tj_sfwd_courses_sortable_columns( $columns ) {
$columns['priority'] = 'priority';
return $columns;
}
/* Only run our customization on the 'edit.php' page in the admin. */
add_action( 'load-edit.php', 'tj_edit_sfwd_courses_load' );
function tj_edit_sfwd_courses_load() {
add_filter( 'request', 'tj_sort_priority' );
}
/* Sorts the priority. */
function tj_sort_priority( $vars ) {
/* Check if we're viewing the 'sfwd-courses' post type. */
if ( isset( $vars['post_type'] ) && 'sfwd-courses' == $vars['post_type'] ) {
/* Check if 'orderby' is set to 'priority'. */
if ( isset( $vars['orderby'] ) && 'priority' == $vars['orderby'] ) {
/* Merge the query vars with our custom variables. */
$vars = array_merge(
$vars,
array(
'meta_key' => 'sfwd-display-priority',
'orderby' => 'meta_value_num'
)
);
}
}
return $vars;
}
?>