Reputation: 331
I searched a lot how to customize custom posts list, I managed to add a new columns with manage_commercial_cpi_posts_columns, But, I need to remove the "title" from the list, and still have the edit, delete options. How can I do it?
Upvotes: 3
Views: 4655
Reputation: 6828
You could hide the title link with CSS or JS, but here's a PHP approach. First remove the title column, and add a custom one:
add_filter( 'manage_commercial_cpi_posts_columns', 'manage_commercial_cpi_posts_columns', 25, 1 );
function manage_commercial_cpi_posts_columns( $cols )
{
// remove title column
unset( $cols['title'] );
// add custom column
$cols['ccpi_links'] = __( 'Edit', 'textdomain' );
// return columns
return $cols;
}
Then, use the row_actions method of the WP_List_Table class to generate the links. Code is copied from the WP_Posts_List_Table class. An important detail is the addition of true
as the second parameter of the row_actions
method, ensuring the links are always visible.
add_action( 'manage_commercial_cpi_posts_custom_column', 'manage_commercial_cpi_custom_column', 10, 2 );
function manage_commercial_cpi_custom_column( $col, $post_id )
{
switch( $col ) :
case 'ccpi_links' :
// variables
$post = get_post( $post_id );
$title = _draft_or_post_title();
$post_type_object = get_post_type_object( $post->post_type );
$can_edit_post = current_user_can( 'edit_post', $post->ID );
// set up row actions
$actions = array();
if ( $can_edit_post && 'trash' != $post->post_status ) {
$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item' ) ) . '">' . __( 'Edit' ) . '</a>';
$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr( __( 'Edit this item inline' ) ) . '">' . __( 'Quick Edit' ) . '</a>';
}
if ( current_user_can( 'delete_post', $post->ID ) ) {
if ( 'trash' == $post->post_status )
$actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
elseif ( EMPTY_TRASH_DAYS )
$actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
$actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently' ) . "</a>";
}
if ( $post_type_object->public ) {
if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
if ( $can_edit_post ) {
$preview_link = set_url_scheme( get_permalink( $post->ID ) );
/** This filter is documented in wp-admin/includes/meta-boxes.php */
$preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
$actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
}
} elseif ( 'trash' != $post->post_status ) {
$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
}
}
// invoke row actions
$table = new WP_Posts_List_Table;
echo $table->row_actions( $actions, true );
break;
endswitch;
}
result
Upvotes: 2