最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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? :)

发布评论

评论列表(0)

  1. 暂无评论