0

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

6
  • 1
    what action/priority does this code run on?
    – Milo
    Commented Nov 17, 2015 at 21:36
  • 1
    Is the code you posted the function hooked to the filter?
    – jdm2112
    Commented Nov 17, 2015 at 21:45
  • Milo, priority is the default 10 I guess.. The action is to just add an extra column to display the shortlink in the taxonomy specified by 'manage_edit-'.$taxonomy.'_columns'. I have not included this as it works fine when I dont place the filter inside the loop.
    – harman
    Commented Nov 18, 2015 at 13:24
  • Unless I misunderstood your question, the function hooked to the filter is the add_shortlink_column function which works fine if I don't put the filter inside the loop. As mentioned above the function just adds an extra column in the categories defined by 'manage_edit-'.$taxonomy.'_columns'
    – harman
    Commented Nov 18, 2015 at 13:26
  • I meant what action does the code as a whole run on? get_taxonomies() won't work until after all taxonomies are registered, so later than init or on init with a very late priority. If this runs when the plugin file or theme functions.php file is loaded, that's too early.
    – Milo
    Commented Nov 18, 2015 at 23:00

1 Answer 1

1

As per the suggestion of Milo who pointed me to the right direction, this problem has now been resolved.

All I had to do was this...

add_action( 'init', 'get_custom_tax' );
function get_custom_tax() {
  $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', 50, 1 );     
  } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.