custom post types - Replacing the title in admin list table

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&

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?

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?

Share Improve this question edited Jul 8, 2014 at 14:11 Pipo asked Jul 8, 2014 at 13:20 PipoPipo 6092 gold badges11 silver badges27 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 27

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 );
}

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);

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339057a4623240.html

相关推荐

  • custom post types - Replacing the title in admin list table

    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&

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信