Using WordPress ‘print_column_headers()’ PHP function

The print_column_headers() WordPress PHP function prints column headers for a particular screen.

Usage

print_column_headers( $screen, $with_id = true );

Parameters

  • $screen (string|WP_Screen) – The screen hook name or screen object, required.
  • $with_id (bool) – Whether to set the ID attribute or not, optional. Default: true.

More information

See WordPress Developer Resources: print_column_headers()

Examples

Display Column Headers for Posts Screen

// Add an action to display column headers for the posts screen
add_action('admin_init', 'display_posts_screen_headers');

function display_posts_screen_headers() {
    // Get the current screen object
    $screen = get_current_screen();

    if ($screen->id == 'edit-post') {
        // Print the column headers for the posts screen
        print_column_headers($screen);
    }
}

Display Column Headers Without ID Attribute

// Add an action to display column headers without ID attribute for the comments screen
add_action('admin_init', 'display_comments_screen_headers_no_id');

function display_comments_screen_headers_no_id() {
    // Get the current screen object
    $screen = get_current_screen();

    if ($screen->id == 'edit-comments') {
        // Print the column headers for the comments screen without ID attribute
        print_column_headers($screen, false);
    }
}

Add Custom Column Headers to Pages Screen

// Add a filter to modify the column headers for the pages screen
add_filter('manage_pages_columns', 'add_custom_pages_screen_headers');

function add_custom_pages_screen_headers($columns) {
    // Add a custom column header
    $columns['custom_column'] = __('Custom Column');

    return $columns;
}

// Add an action to display the custom column content
add_action('manage_pages_custom_column', 'display_custom_pages_column_content', 10, 2);

function display_custom_pages_column_content($column_name, $post_id) {
    if ($column_name == 'custom_column') {
        echo 'Custom content for post ID ' . $post_id;
    }
}

Add Custom Column Headers to Media Library Screen

// Add a filter to modify the column headers for the media library screen
add_filter('manage_media_columns', 'add_custom_media_screen_headers');

function add_custom_media_screen_headers($columns) {
    // Add a custom column header
    $columns['custom_column'] = __('Custom Column');

    return $columns;
}

// Add an action to display the custom column content
add_action('manage_media_custom_column', 'display_custom_media_column_content', 10, 2);

function display_custom_media_column_content($column_name, $post_id) {
    if ($column_name == 'custom_column') {
        echo 'Custom content for media ID ' . $post_id;
    }
}

Display Column Headers for Custom Post Type Screen

// Add an action to display column headers for a custom post type screen
add_action('admin_init', 'display_custom_post_type_screen_headers');

function display_custom_post_type_screen_headers() {
    // Get the current screen object
    $screen = get_current_screen();

    if ($screen->id == 'edit-custom_post_type') {
        // Print the column headers for the custom post type screen
        print_column_headers($screen);
    }
}