The mbct_{$this->model->name}_column_output filter
Support › MB Custom Table › The mbct_{$this->model->name}_column_output filterResolved
- This topic has 2 replies, 2 voices, and was last updated 2 years, 1 month ago by
James Cooper.
-
AuthorPosts
-
March 18, 2023 at 2:40 AM #41078
James Cooper
ParticipantI'm using the mbct_{$this->model->name}_column_output filter to modify the output of a column for a custom model, however, the modified output is not displaying in the admin list table. Do you know why that might be?
Here is my code:
/** * Modify the output of the 'title' column for the 'import' custom model to include the 'edit' link. * * @param mixed $output Column output. * @param string $column_name Column name. * @param array $item Item data. * @param object $model Model instance. * * @return mixed Modified column output. */ add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 10, 4); function bcc_modify_title_column_output($output, $column_name, $item, $model) { if ($column_name === 'title') { $base_url = admin_url("admin.php?page=model-import"); $edit_url = add_query_arg([ 'model-action' => 'edit', 'model-id' => $item['ID'], ], $base_url); // Make the title a link to the 'edit' page. $output = sprintf('<a href="%s">%s</a>', esc_url($edit_url), esc_html($output)); } return $output; }
March 18, 2023 at 9:03 PM #41091Peter
ModeratorHello,
I've tried to run the code on my local site and see there are two issues:
- You should run the callback function with a higher priority like 99
add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 99, 4);
- You should return the HTML code instead of printing it out, because in the WordPress list table function, it is outputted by using
echo
state. Please check the filewp-admin/includes/class-wp-list-table.php
line 1532. It should be:// Make the title a link to the 'edit' page. $output = '<a href="' . esc_url($edit_url) . '">' . $output . '</a>';
Let me know how it goes.
March 21, 2023 at 5:17 AM #41135James Cooper
ParticipantPerfect! Works great now!
Here is the updated code:
/** * Modify the output of the 'title' column for the 'import' custom model to include the 'edit' link. * * @param mixed $output Column output. * @param string $column_name Column name. * @param array $item Item data. * @param object $model Model instance. * * @return mixed Modified column output. */ add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 99, 4); function bcc_modify_title_column_output($output, $column_name, $item, $model) { if ($column_name === 'title') { $base_url = admin_url('admin.php?page=model-import'); $edit_url = add_query_arg( [ 'model-action' => 'edit', 'model-id' => $item['ID'], ], $base_url ); // Make the title a link to the 'edit' page. $output = '<a href="' . esc_url($edit_url) . '">' . $output . '</a>'; } return $output; }
Thank you Peter!
-
AuthorPosts
- You must be logged in to reply to this topic.