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?
3 Answers
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:
-
Based on the
manage_{$this->screen->id}_columns
hookadd_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; }
-
Based on the
manage_{$post_type}_posts_columns
hookadd_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 );
}