Custom plugin: Loop through taxonomy types and update columns for all types?

Question

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?

0
, Joel Hoelting 3 years 2020-04-01T20:51:19-05:00 0 Answers 74 views 0

Leave an answer

Browse
Browse