Using WordPress ‘get_hidden_columns()’ PHP function

The get_hidden_columns() WordPress PHP function retrieves a list of hidden columns for a specific screen.

Usage

$hidden_columns = get_hidden_columns($screen);

Parameters

  • $screen (string|WP_Screen) – Required. The screen you want the hidden columns for.

More information

See WordPress Developer Resources: get_hidden_columns()

Examples

Get hidden columns for a custom post type screen

In this example, we will get the hidden columns for a custom post type screen called “books”.

// Get the WP_Screen object for the custom post type "books"
$screen = get_current_screen();

// Get the hidden columns for the custom post type "books"
$hidden_columns = get_hidden_columns($screen);

// Output the hidden columns
foreach ($hidden_columns as $column) {
    echo "Hidden column: " . $column . "<br>";
}

Check if a specific column is hidden on the “posts” screen

In this example, we will check if the “comments” column is hidden on the “posts” screen.

// Get the WP_Screen object for the "posts" screen
$screen = get_current_screen();

// Get the hidden columns for the "posts" screen
$hidden_columns = get_hidden_columns($screen);

// Check if the "comments" column is hidden
if (in_array('comments', $hidden_columns)) {
    echo "The 'comments' column is hidden.";
} else {
    echo "The 'comments' column is visible.";
}

Display the number of hidden columns on the “pages” screen

In this example, we will display the number of hidden columns on the “pages” screen.

// Get the WP_Screen object for the "pages" screen
$screen = get_current_screen();

// Get the hidden columns for the "pages" screen
$hidden_columns = get_hidden_columns($screen);

// Display the number of hidden columns
echo "There are " . count($hidden_columns) . " hidden columns on the 'pages' screen.";

Get hidden columns for a custom admin page

In this example, we will get the hidden columns for a custom admin page with a screen ID “my_custom_page”.

// Get the WP_Screen object for the custom admin page with screen ID "my_custom_page"
$screen = WP_Screen::get('my_custom_page');

// Get the hidden columns for the custom admin page
$hidden_columns = get_hidden_columns($screen);

// Output the hidden columns
foreach ($hidden_columns as $column) {
    echo "Hidden column: " . $column . "<br>";
}

Hide a specific column if it’s not already hidden

In this example, we will hide the “categories” column on the “posts” screen if it’s not already hidden.

// Get the WP_Screen object for the "posts" screen
$screen = get_current_screen();

// Get the hidden columns for the "posts" screen
$hidden_columns = get_hidden_columns($screen);

// Hide the "categories" column if it's not already hidden
if (!in_array('categories', $hidden_columns)) {
    $hidden_columns[] = 'categories';
    update_hidden_columns($screen, $hidden_columns);
    echo "The 'categories' column has been hidden.";
} else {
    echo "The 'categories' column is already hidden.";
}