Skip to content

Instantly share code, notes, and snippets.

@Aziz-Rahman
Last active December 31, 2015 02:52
Show Gist options
  • Save Aziz-Rahman/17580a3919ae2fbe96b0 to your computer and use it in GitHub Desktop.
Save Aziz-Rahman/17580a3919ae2fbe96b0 to your computer and use it in GitHub Desktop.
Sub menu custom posttype and display data based empty photo in page template photo
<?php
/*------------------------------------------------------------*/
/* Register Taxonomy */
/*------------------------------------------------------------*/
if ( ! function_exists( 'my_image_color' ) ) :
// Register Photo Tag Taxonomy
function my_image_color() {
$labels = array(
'name' => _x( 'Photo Colors', 'taxonomy general name' ),
'singular_name' => _x( 'Photo Color', 'taxonomy singular name' ),
'search_items' => __( 'Search Photo Colors' ),
'popular_items' => __( 'Popular Photo Colors' ),
'all_items' => __( 'All Photo Colors' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Photo Color' ),
'update_item' => __( 'Update Photo Color' ),
'add_new_item' => __( 'Add New Photo Color' ),
'new_item_name' => __( 'New Photo Color Name' ),
'separate_items_with_commas' => __( 'Separate HTML color codes with commas' ),
'add_or_remove_items' => __( 'Add or remove HTML color codes' ),
'choose_from_most_used' => __( 'Choose from the most used HTML color codes' ),
'menu_name' => __( 'Photo Colors' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'photo-color' )
);
register_taxonomy( 'photo-color', array( 'photo' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'my_image_color', 0 );
endif;
// =================== SUB MENU CUSTOM POST TYPE ========================
// WP_List_Table
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Empty_Photo extends WP_List_Table {
/**
* Constructor, we override the parent to pass our own arguments
* We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
*/
function __construct() {
parent::__construct( array(
'singular' => 'photo_empty', //Singular label
'plural' => 'photo_empty', //plural label, also this well be one of the table css class
'ajax' => false //We won't support Ajax for this table
) );
}
function admin_header() {
$page = ( isset($_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false;
if( 'empty-feature-image' != $page )
return;
}
function ajax_user_can() {
return current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_posts );
}
function no_items() {
if ( isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] )
echo get_post_type_object( $this->screen->post_type )->labels->not_found_in_trash;
else
echo get_post_type_object( $this->screen->post_type )->labels->not_found;
}
function get_edit_link( $args, $label, $class = '' ) {
$url = add_query_arg( $args, 'edit.php' );
$class_html = '';
if ( ! empty( $class ) ) {
$class_html = sprintf(
' class="%s"',
esc_attr( $class )
);
}
return sprintf(
'<a href="%s"%s>%s</a>',
esc_url( $url ),
$class_html,
$label
);
}
function table_nav( $which ) {
global $cat;
?>
<div class="alignleft actions">
<?php
if ( 'top' === $which && !is_singular() ) {
$this->months_dropdown( $this->screen->post_type );
if ( is_object_in_taxonomy( $this->screen->post_type, 'category' ) ) {
$dropdown_options = array(
'show_option_all' => __( 'All categories' ),
'hide_empty' => 0,
'hierarchical' => 1,
'show_count' => 0,
'orderby' => 'name',
'selected' => $cat
);
echo '<label class="screen-reader-text" for="cat">' . __( 'Filter by category' ) . '</label>';
wp_dropdown_categories( $dropdown_options );
}
/**
* Fires before the Filter button on the Posts and Pages list tables.
*
* The Filter button allows sorting by date and/or category on the
* Posts list table, and sorting by date on the Pages list table.
*
* @since 2.1.0
* @since 4.4.0 The `$post_type` parameter was added.
*
* @param string $post_type The post type slug.
*/
do_action( 'restrict_manage_posts', $this->screen->post_type );
submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
}
if ( $this->is_trash && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) ) {
submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
}
?>
</div>
<?php
/**
* Fires immediately following the closing "actions" div in the tablenav for the posts
* list table.
*
* @since 4.4.0
*
* @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
*/
do_action( 'manage_posts_extra_tablenav', $which );
}
function current_action_tbl() {
if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
return 'delete_all';
return parent::current_action_tbl();
}
function get_table_classes() {
return array( 'widefat', 'fixed', 'striped', is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts' );
}
function column_default($item, $column_name) {
switch ($column_name) {
case 'post_thumbnail':
return get_the_post_thumbnail( $item->ID , array( 40, 40) );
case 'post_title':
$edit_link = get_edit_post_link($item->ID);
return '<a href="'.$edit_link.'">'.$item->$column_name.'</a>';
case 'post_author':
return '<a href="'.get_author_posts_url($item->$column_name).'">'.get_the_author_meta( 'display_name', $item->$column_name ).'</a>';
case 'photo_source':
$term_source = wp_get_post_terms($item->ID, 'source', array("fields" => "all"));
if (!empty($term_source)) {
return $term_source[0]->name;
}
case 'photo_cat':
$term_cat = wp_get_post_terms($item->ID, 'photo-category', array("fields" => "all"));
if (!empty($term_cat)) {
return $term_cat[0]->name;
}
case 'photo_tag':
$photo_tag = get_the_term_list( $item->ID, 'photo-tag', '', ', ', '' );
return $photo_tag;
case 'photo_color':
$photo_color = get_the_term_list( $item->ID, 'photo-color', '', ', ', '' );
return $photo_color;
case 'post_date':
return $item->$column_name;
default:
return print_r($item, true); //Show the whole array for troubleshooting purposes
}
}
function get_columns() {
return $columns= array(
// 'empty_titles_cb' => '<input type="checkbox" />',
'post_title' =>__('Title'),
'post_thumbnail' =>__('Image'),
'post_author' =>__('Author'),
'photo_source' =>__('Site Source'),
'photo_cat' =>__('Photo Categories'),
'photo_tag' =>__('Photo Tags'),
'photo_color' =>__('Photo Color'),
'post_date' =>__('Date')
);
}
function column_list( $item ) {
if ( current_user_can( 'edit_post', $item->ID ) ): ?>
<label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php
printf( __( 'Select %s' ), _draft_or_post_title() );
?></label>
<input id="cb-select-<?php the_ID(); ?>" type="checkbox" name="photo[]" value="<?php the_ID(); ?>" />
<div class="locked-indicator"></div>
<?php endif;
}
function action_post_list($item) {
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&photo=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
'delete' => sprintf('<a href="?page=%s&action=%s&photo=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),
);
return sprintf('%1$s %2$s', $item['post_title'], $this->row_actions($actions) );
}
function get_bulk_actions() {
$actions = array();
$post_type_obj = get_post_type_object( $this->screen->post_type );
if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
if ( $this->is_trash ) {
$actions['untrash'] = __( 'Restore' );
} else {
$actions['edit'] = __( 'Edit' );
}
}
if ( current_user_can( $post_type_obj->cap->delete_posts ) ) {
if ( $this->is_trash || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = __( 'Delete Permanently' );
} else {
$actions['trash'] = __( 'Move to Trash' );
}
}
return $actions;
}
/**
* Decide which columns to activate the sorting functionality on
* @return array $sortable, the array of columns that can be sorted by the user
*/
function get_sortable_columns() {
return $sortable = array(
'post_title'=>'post_title',
'post_author'=>'post_author',
'post_date'=>'post_date'
);
}
/**
* Prepare the table with different parameters, pagination, columns and table elements
*/
function list_item_empty_photo() {
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
/* -- Preparing your query -- */
$query = "SELECT wp_posts.* FROM wp_posts
LEFT JOIN wp_postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '_thumbnail_id' )
WHERE $wpdb->posts.post_type = 'photo' AND ( $wpdb->posts.post_status = 'publish' ) AND ( $wpdb->postmeta.post_id IS NULL )";
/* -- Ordering parameters -- */
//Parameters that are going to be used to order the result
$orderby = !empty($_GET["orderby"]) ? $_GET["orderby"] : 'post_date';
$order = !empty($_GET["order"]) ? $_GET["order"] : '';
if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
/* -- Pagination parameters -- */
//Number of elements in your table?
$totalitems = $wpdb->query($query); //return the total number of affected rows
//How many to display per page?
$perpage = 10;
//Which page is this?
$paged = !empty($_GET["paged"]) ? $_GET["paged"] : '';
//Page Number
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
//How many pages do we have in total?
$totalpages = ceil($totalitems/$perpage);
//adjust the query to take pagination into account
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=' LIMIT '.(int)$offset.','.(int)$perpage;
}
/* -- Register the pagination -- */
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
) );
//The pagination links are automatically built according to those parameters
/* -- Register the Columns -- */
$columns = $this->get_columns();
$_wp_column_headers[$screen->id]=$columns;
/* -- Fetch the items -- */
$this->items = $wpdb->get_results($query);
}
}
if ( ! function_exists( 'my_empty_feature_image' ) ) :
function my_empty_feature_image(){
$hook = add_submenu_page( 'edit.php?post_type=photo', 'Empty Photo', 'Empty Photo', 'edit_posts', 'empty-photo', 'list_page_empty_photo' );
add_action( "load-$hook", 'option_empty_photo' );
}
add_action( 'admin_menu', 'my_empty_feature_image' );
endif;
function option_empty_photo() {
global $wp_list_table;
$option = 'per_page';
$args = array(
'label' => 'Empty Photo',
'default' => 10,
'option' => 'books_per_page'
);
add_screen_option( $option, $args );
$wp_list_table = new Empty_Photo();
}
add_action( 'admin_menu', 'my_empty_feature_image' );
function list_page_empty_photo(){
global $wp_list_table;
echo '</pre><div class="wrap"><h2>List Empty Photo</h2>';
$wp_list_table->list_item_empty_photo();
?>
<form method="post">
<input type="hidden" name="page" value="photo-empty-titles">
<?php
$wp_list_table->search_box( 'search', 'search_id' );
$wp_list_table->display();
echo '</form></div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment