I have a taxonomy called "dog" and multiple dog types:
"dog": {
"name": "Dog",
"slug": "dog",
"description": "",
"types": [
"poodle",
"retriever",
"labrador",
...etc
],
}
Each dog type has an image custom field and I am writing a custom plugin that adds a thumbnail column to each row. Here is my code and you can see there is unnecessary repetition:
function update_poodle_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'image' => __( 'Image' ),
'date' => __( 'Date' )
);
return $columns;
}
add_filter( 'manage_poodle_posts_columns', 'update_poodle_columns' );
function update_poodle_column( $column, $post_id ) {
switch ( $column ) {
case 'image':
$img_array = get_field('image');
$img = $img_array['sizes']['thumbnail'];
echo '<img src="' . $img . '" height="100px" width="100px" />';
break;
case 'year':
echo get_field( 'year', $post_id );
break;
}
}
add_action( 'manage_poodle_posts_custom_column', 'update_poodle_column', 10, 2);
function update_retriever_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'image' => __( 'Image' ),
'date' => __( 'Date' )
);
return $columns;
}
add_filter( 'manage_retriever_posts_columns', 'update_retriever_columns' );
function update_25_retriever_column( $column, $post_id ) {
switch ( $column ) {
case 'image':
$img_array = get_field('image');
$img = $img_array['sizes']['thumbnail'];
echo '<img src="' . $img . '" height="100px" width="100px" />';
break;
case 'year':
echo get_field( 'year', $post_id );
break;
}
}
add_action( 'manage_retriever_posts_custom_column', 'update_retriever_column', 10, 2);
I would like to avoid repeating the same code for every single post type. How would I loop through taxonomy types and call the function dynamically?
I have a taxonomy called "dog" and multiple dog types:
"dog": {
"name": "Dog",
"slug": "dog",
"description": "",
"types": [
"poodle",
"retriever",
"labrador",
...etc
],
}
Each dog type has an image custom field and I am writing a custom plugin that adds a thumbnail column to each row. Here is my code and you can see there is unnecessary repetition:
function update_poodle_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'image' => __( 'Image' ),
'date' => __( 'Date' )
);
return $columns;
}
add_filter( 'manage_poodle_posts_columns', 'update_poodle_columns' );
function update_poodle_column( $column, $post_id ) {
switch ( $column ) {
case 'image':
$img_array = get_field('image');
$img = $img_array['sizes']['thumbnail'];
echo '<img src="' . $img . '" height="100px" width="100px" />';
break;
case 'year':
echo get_field( 'year', $post_id );
break;
}
}
add_action( 'manage_poodle_posts_custom_column', 'update_poodle_column', 10, 2);
function update_retriever_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'image' => __( 'Image' ),
'date' => __( 'Date' )
);
return $columns;
}
add_filter( 'manage_retriever_posts_columns', 'update_retriever_columns' );
function update_25_retriever_column( $column, $post_id ) {
switch ( $column ) {
case 'image':
$img_array = get_field('image');
$img = $img_array['sizes']['thumbnail'];
echo '<img src="' . $img . '" height="100px" width="100px" />';
break;
case 'year':
echo get_field( 'year', $post_id );
break;
}
}
add_action( 'manage_retriever_posts_custom_column', 'update_retriever_column', 10, 2);
I would like to avoid repeating the same code for every single post type. How would I loop through taxonomy types and call the function dynamically?
Share Improve this question asked Apr 1, 2020 at 23:03 Joel HoeltingJoel Hoelting 1375 bronze badges 1- 2 Could you not use the same function for all of them instead of using duplicates? You don't need different functions for each one – Tom J Nowell ♦ Commented Apr 1, 2020 at 23:49
1 Answer
Reset to default 1The only things I see different in your code are the add_filter()
and add_action()
calls (e.g. the callback/function name), so you can use the same functions for all of the types:
function update_dog_type_columns( $columns ) {
// your code
return $columns;
}
function update_dog_type_column( $column, $post_id ) {
// your code
}
And then you can do something like this to register the filter/action callback for each (post) type:
foreach ( array( 'poodle', 'retriever', 'labrador', 'etc' ) as $type ) {
add_filter( "manage_{$type}_posts_columns", 'update_dog_type_columns' );
add_action( "manage_{$type}_posts_custom_column", 'update_dog_type_column', 10, 2 );
}
Alternate Solution
The above is good, but this might be better to make sure your callback doesn't run for other types:
You can hook to just manage_posts_columns
and use the $post_type
parameter to conditionally run your filter/action:
function update_dog_type_columns( $columns, $post_type ) {
if ( in_array( $post_type, array( 'poodle', 'retriever', 'labrador', 'etc' ) ) ) {
// your code
}
return $columns;
}
add_filter( 'manage_posts_columns', 'update_dog_type_columns', 10, 2 );
And then hook to manage_pages_custom_column
and/or manage_posts_custom_column
, depending on whether any of the "dog" post types is hierarchical or not:
function update_dog_type_column( $column, $post_id ) {
$post_type = get_current_screen()->post_type;
if ( ! in_array( $post_type, array( 'poodle', 'retriever', 'labrador', 'etc' ) ) ) {
return;
}
switch ( $column ) {
// your code
}
}
add_action( 'manage_pages_custom_column', 'update_dog_type_column', 10, 2 ); // for hierarchical post types
add_action( 'manage_posts_custom_column', 'update_dog_type_column', 10, 2 ); // non-hierarchical post types
But either way, you would only need just two functions.
So I hope that helps? :)