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?
3 Answers
Reset to default 271. 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 );
}
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