Home >Backend Development >PHP Tutorial >Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen

Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen

Christopher Nolan
Christopher NolanOriginal
2025-02-26 11:22:14214browse

This tutorial demonstrates how to add a featured image column to the WordPress posts screen, and extend this functionality to custom post types. Let's streamline the explanation and improve clarity.

Step 1: Enable Featured Images

First, ensure your theme supports featured images. Open your theme's functions.php file (create it if it doesn't exist) and add the following code:

<code class="language-php">add_theme_support( 'post-thumbnails' );
add_image_size( 'featured_preview', 200, 150, false );</code>

This enables featured image support and creates a custom thumbnail size (featured_preview) for previewing images in the new column. The false argument prevents cropping.

Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen (Image: Featured Image setting in the WordPress post editor)

Step 2: Add Featured Image Column to Posts

Next, add a custom column to display the featured image in the WordPress posts list. Add these functions to your functions.php file:

<code class="language-php">function st4_add_featured_image_column( $defaults ) {
    $defaults['featured_image'] = __( 'Featured Image' );
    return $defaults;
}

function st4_show_featured_image_column( $column_name, $post_ID ) {
    if ( $column_name == 'featured_image' ) {
        $featured_image = get_the_post_thumbnail( $post_ID, 'featured_preview' );
        if ( $featured_image ) {
            echo $featured_image;
        } else {
            echo '<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174054013775854.jpg" class="lazy" alt="Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen "> <em>(Image: Posts list with new Featured Image column)</em>
<p>You can show/hide this column via the <strong>Screen Options</strong> panel.</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174054013887569.jpg" class="lazy" alt="Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen "> <em>(Image: Screen Options panel)</em></p>
<p><strong>Step 3: Extend to Custom Post Types</strong></p>
<p>To add the featured image column to custom post types,  simply replace <code>manage_posts_columns</code> and <code>manage_posts_custom_column</code> with the appropriate custom post type hooks. For example, for a custom post type named "movies":</p>
<pre class="brush:php;toolbar:false"><code class="language-php">add_filter( 'manage_movie_posts_columns', 'st4_add_featured_image_column' );
add_action( 'manage_movie_posts_custom_column', 'st4_show_featured_image_column', 10, 2 );</code>

Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen (Image: Featured image in a Movie post)

Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen (Image: Featured Image column in the Movies custom post type)

Further Considerations (briefly):

  • Targeting Specific Post Types: Use the manage_{post_type}_posts_columns and manage_{post_type}_posts_custom_column hooks to add the column to only specific custom post types.
  • Multiple Columns: Easily add multiple custom columns by expanding the st4_add_featured_image_column and st4_show_featured_image_column functions.
  • Removing Default Columns: Use unset() within the filter to remove default columns.

This revised response provides a more concise and focused explanation, while maintaining the essential information and images. The code is also improved for readability and efficiency.

The above is the detailed content of Add a Custom Column in WordPress Posts and Custom Post Types Admin Screen. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn