7

I've created a custom post type for my "Testimonials", I don't need a title(s), how can I write a function that Adds the "Edit | Quick Edit | Trash | View" links to "date" if title isn't shown in the column.

I'm using the plugin "Admin Columns" to hide the columns I don't wish to show.

base

I'm trying to re-use this code from this previous StackExchange question ( No Edit / Delete Links for Custom Post Type? ), however I have already created the fields "Author" etc.

Using this code gives this result.

result

2 Answers 2

3

This is not easily done as there is no hook to add the rows actions. You can however, unregister the date column and re-register your own date column with the row actions added. A bit hacky unfortunately.

I have tried to ensure that the following code is for post type 'testimonials'

First register your new date column and unregister the old: (use manage_{post_type}_posts_columns hook)

add_filter('manage_testimonials_posts_columns', 'my_custom_date_column_head'); 
function my_custom_date_column_head($columns) {  
    $columns['date2'] = 'Date';  
    unset( $columns['date'] );
    return $columns;  
}

Then make your new date column sortable, (by 'date'). Use manage_edit-{post_type}_sortable_columns hook)

add_filter( 'manage_edit-testimonials_sortable_columns', 'my_custom_date_column_sort' );
function my_custom_date_column_sort( $columns ) {
    $columns['date2'] = 'date';
    return $columns;
}

Now comes the fun bit - displaying the content of the column. I have pretty much copied and pasted what WordPress does to fill the date column and then added the actions onto the end.

Use manage_{post_type}_posts_custom_columns hook)

add_action( "manage_testimonials_posts_custom_column", 'my_custom_date_column_content',10,2);
function my_custom_date_column_content($column, $post_id ){
    global $post,$mode;

    if( 'date2' != $column )
        return;

    //**** Display default content of date column *******//

    if ( '0000-00-00 00:00:00' == $post->post_date ) {
        $t_time = $h_time = __( 'Unpublished' );
        $time_diff = 0;
    } else {
        $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
        $m_time = $post->post_date;
        $time = get_post_time( 'G', true, $post );

        $time_diff = time() - $time;

        if ( $time_diff > 0 && $time_diff < 24*60*60 )
            $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
        else
            $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
    }

    if ( 'excerpt' == $mode )
        echo apply_filters( 'post_date_column_time', $t_time, $post, $column, $mode );
    else
        echo '<abbr title="' . $t_time . '">' . apply_filters( 'post_date_column_time', $h_time, $post, $column, $mode ) . '</abbr>';

    echo '<br />';

    if ( 'publish' == $post->post_status ) {
        _e( 'Published' );

    } elseif ( 'future' == $post->post_status ) {
        if ( $time_diff > 0 )
            echo '<strong class="attention">' . __( 'Missed schedule' ) . '</strong>';
        else
        _e( 'Scheduled' );

    } else {
        _e( 'Last Modified' );
    }

    //***** END  -- Display default content of date column *******//


    //***** START  -- Our actions  *******//

    //First set up some variables
    $actions = array();
    $post_type_object = get_post_type_object( $post->post_type );
    $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );


    //Actions to edit
    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&nbsp;Edit' ) . '</a>';
    }

    //Actions to delete/trash
    if ( current_user_can( $post_type_object->cap->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 . '&amp;action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $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>";
    }

    //Actions to view/preview
        if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
            if ( $can_edit_post )
                $actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';

        } elseif ( 'trash' != $post->post_status ) {
                $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
        }

    //***** END  -- Our actions  *******//

    //Echo the 'actions' HTML, let WP_List_Table do the hard work
    echo WP_List_Table::row_actions( $actions );
}
11
  • Thanks so much for your response, I tried copying this code to my functions.php file however It did not work. What would you suggest I try?
    – Josh
    Mar 29, 2012 at 2:36
  • In what sense did not work? Did the new column not appear? Did it not containing anything, did it nor sort? Put debug mode on while developing as this will help identify errors. Mar 29, 2012 at 7:45
  • The date column showed up but that's it.
    – Josh
    Mar 29, 2012 at 7:46
  • And what is the error with debug mode on? Mar 29, 2012 at 7:56
  • When I use debug mode eg. (localhost/salwaydesigns/wp-admin/…) it says "Invalid post type" before and after implementing your code. I will have to correct the testimonials post type.. What would cause this?
    – Josh
    Mar 29, 2012 at 8:32
5

I know this thread is old, but i add a better answer working since Wordpress 4.3. You can use a filter to assign the primary column of the table. For this solution you need to know the Post Type, and the column ID (inspect the html with your browser to find it). In my example the post type is "event" (so the variable $screen is "edit-event"), and the column is a custom taxonomy "event-places" (WP is calling the column "taxonomy-event-places").

add_filter( 'list_table_primary_column', 'fix_actions_primary_column', 10, 2 );
function fix_actions_primary_column( $default, $screen ) {
    if ( 'edit-event' === $screen ) {
        $default = 'taxonomy-event-places';
    }
    return $default;
}
1
  • This is a much better answer. Thanks!
    – Gavin
    Jun 26, 2019 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.