I am trying to create a filter to add a custom column to the edit page of any custom category that might exist in Wordpress. I am aware that the filter hook must be in the form, manage_edit-{$cat}_columns
. Lets say that this is the function to add:
/* Add custom column to lists */
function add_shortlink_column( $columns ) {
return array_merge( $columns,
array( 'shortlink' => __( 'Shortlink', 'your_text_domain' ) ) );
}
Because I want to cover ALL custom categories (current or future) I must do this using a foreach loop after calling get_taxonomies.
However add_filter is causing me problems...
Suppose that I have only two categories; topics and product_cat.
If I try this everything works fine and the column is added:
$value1 = 'topics';
$value2 = 'product_cat';
$filters = array( "manage_edit-{$value1}_columns", "manage_edit-{$value2}_columns" );
foreach ( $filters as $filter ) {
add_filter( $filter, 'add_shortlink_column' );
}
However, if I try this it does not work.
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies = get_taxonomies( $args, 'names', 'and' );
foreach($taxonomies as $key => $taxonomy) {
$filter = "manage_edit-{$taxonomy}_columns";
add_filter( $filter, 'add_shortlink_column' );
}
I am pretty sure that this is some sort of syntax problem. But I cannot figure it out. Any help would be much appreciated.
Thanks
get_taxonomies()
won't work until after all taxonomies are registered, so later thaninit
or oninit
with a very late priority. If this runs when the plugin file or themefunctions.php
file is loaded, that's too early.