10

Here is my situation: I am trying to filter the content of the title column in my custom post type edit table but I can't get it working.

Here is what I have tried:

add_filter('manage_edit-mycpt_columns', 'replace_title_products');

function replace_title_products() {
    $oldtitle = get_the_title();
    $newtitle = str_replace(array("<span class='sub-title'>", "</span>"), array("", ""),$oldtitle);
    $title = esc_attr($newtitle);
    return $title;  
}

I just want to filter the <span> tags in my title. Could someone help me please?

19

1. Change post title in post list column

I misunderstood what you wanted - obviously. You can do that like this:

add_action(
    'admin_head-edit.php',
    'wpse152971_edit_post_change_title_in_list'
);
function wpse152971_edit_post_change_title_in_list() {
    add_filter(
        'the_title',
        'wpse152971_construct_new_title',
        100,
        2
    );
}

function wpse152971_construct_new_title( $title, $id ) {
    //print_r( $title );
    //print_r( $id );
    return 'new';
}

Making use of the admin_head-$hook_suffix hook.


(Disclaimer: Keeping this, because it is related and good information)

2. Replace the table column title

Besides you are not using and overwriting the column table title. Below some exemplary code on how to do it:

  1. Based on the manage_{$this->screen->id}_columns hook

    add_filter(
        'manage_edit-post_columns',
        'wpse152971_replace_column_title_method_a'
    );
    function wpse152971_replace_column_title_method_a( $columns ) {  
        //print_r($columns);  
        $columns[ 'title' ] = 'new title';  
        return $columns;  
    }  
    
  2. Based on the manage_{$post_type}_posts_columns hook

    add_filter(
        'manage_post_posts_columns',
        'wpse152971_replace_column_title_method_b'
    );
    function wpse152971_replace_column_title_method_b( $posts_columns ) {
        //print_r($posts_columns);
        $posts_columns[ 'title' ] = 'new title';
        return $posts_columns;
    }
    

Last but not least the following code is handy to get the information you need:

add_action( 'admin_head', 'wpse152619_dbg_dev' );
function wpse152619_dbg_dev() {
    global $pagenow;
    print_r( $pagenow );
    echo '<br>';
    print_r( $_GET[ 'taxonomy' ] );
    echo '<br>';
    $current_screen = get_current_screen();
    print_r( $current_screen->id );
}
|improve this answer|||||
  • Sorry I forget the return line in my code... Thanks for your answer but this is not what I'm trying to achieve. I don't want to change the name of the column for the title, I want to change the content that is returned in this column for each post. Right now I have: "This my title <span class="sub-title">number 1</span>" for first row then "This my title <span class="sub-title">number 2</span>" for second row, etc. Is it more clear what I'm trying to achieve? – Pipo Jul 8 '14 at 14:14
  • Awesome!! That is exactly what I wanted to do. Thanks a lot! – Pipo Jul 8 '14 at 14:57
  • i Tried Solution 1 1. Change post title in post list column Values are Changes, How can i Rename the Label As well . ? – Developer Jun 1 '17 at 7:17
  • @Developer You mean the table column title? Like described under second. – Nicolai Jun 1 '17 at 10:26
  • 1
    I was trying to do the same but for a custom TAXONOMY table header (which need to be shortened, as I have 10+). You can give a shorthand for the admin column 'menu_name' in its 'labels' array, but strangely that doesn't affect the table head. I searched a lot and this article kept coming up, nothing for taxonomies... SO: With your Method 2.1 =A, it can be done (columns named 'taxonomy-{my_tax_name}'), with your Method 2.2 =B you only get 'title' and 'date' and self-inserted columns as the print_r reveals. – user3445853 Nov 26 '19 at 9:16
1

I have just done something similar a few hours ago, so my code might not be the best it could be but you need to use 2 hooks to achieve this. As you appear to be using a custom post type from what I saw in your code, these two hooks would be.

manage_post_type_posts_columns()

manage_post_type_posts_custom_column()

I have used the manage_post_type_posts_columns() filter hook to create a new Title column and unset the old one and then the manage_post_type_posts_custom_column() action hook to use my own method for generating the new content/title for this column.

Hope this helps, have added your code in as well...

// Replace your Title Column with the Existing one //
function replace_title_column($columns) {

    $new = array();

    foreach($columns as $key => $title) {
        if ($key=='title') 
        $new['new-title'] = 'New Title'; // Our New Colomn Name
        $new[$key] = $title;
    }

    unset($new['title']); 
    return $new;
}

// Replace the title with your custom title
function replace_title_products($column_name, $post_ID) {
    if ($column_name == 'new-title') {
        $oldtitle = get_the_title();
        $newtitle = str_replace(array("<span class='sub-title'>", "</span>"), array("", ""),$oldtitle);
        $title = esc_attr($newtitle); 
        echo $title; 
    }
}

add_filter('manage_mycpt_columns', 'replace_title_column');
add_action('manage_mycpt_custom_column', 'replace_title_products', 10, 2);
|improve this answer|||||
  • Thanks Matt. Do you know if there is a way to just filter the original "title" column before the content gets returned? I don't really want to create a new title column as the original one comes with a lot of functions (edit, quick edit, trash, etc.). – Pipo Jul 8 '14 at 14:28
  • @Etienne I'm afraid not. Maybe someone else can give us an idea on how to go about doing this, would also like to know :-) – Matt Royal Jul 8 '14 at 14:37
  • See my revised answer, no need to create a new column. @Etienne – Nicolai Jul 8 '14 at 14:44
0

Replace Columns

Here is an example that completely replaces the columns, rather than adding and removing specific ones

function set_book_columns($columns) {
    return array(
        'cb' => '<input type="checkbox" />',
        'title' => __('Title'),
        'comments' => '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>',
        'date' => __('Date'),
        'publisher' => __('Publisher'),
        'book_author' =>__( 'Book Author')
    );
}
add_filter('manage_book_posts_columns' , 'set_book_columns');

See more:manage_$post_type_posts_columns

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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