Function and usage of get_hidden_columns() in wordpress

Question

Answers ( 1 )

    0
    2024-01-07T01:30:32+00:00

    The get_hidden_columns() function in WordPress is a utility that helps in managing the display of columns on different screens within the WordPress admin area. This function is particularly useful for customizing the admin interface, especially when dealing with custom post types or customizing existing admin screens. Let's break down the function and its usage:

    Function: get_hidden_columns()

    Purpose:

    To retrieve a list of hidden columns for a specific screen in the WordPress admin area.

    Parameters:

    • $screen (string | WP_Screen) [required]:
      • This parameter specifies the screen for which you want to obtain the hidden columns.
      • It can be either a screen ID as a string or an instance of the WP_Screen class.

    Return Value:

    • Type: string[] (Array of strings)
    • Description: The function returns an array containing the IDs of the columns that are currently set to be hidden on the specified screen.

    Sample Usage:

    Scenario:

    Suppose you have a custom post type 'Books' and you want to find out which columns are hidden by default or by user preference in the admin list table for 'Books'.

    Steps:

    1. Get the Screen Object:

      • First, you need to get the WP_Screen object for the 'Books' list table. This can usually be done by using get_current_screen() function if you're hooking into an admin action for the 'Books' screen.
      $screen = get_current_screen();
      
    2. Use get_hidden_columns():

      • Now, pass this $screen object (or just the screen ID if you have it) to the get_hidden_columns() function.
      $hidden_columns = get_hidden_columns($screen);
      
    3. Process the Result:

      • The $hidden_columns variable will now contain an array of column IDs that are hidden. You can loop through this array to process or display the hidden column IDs.
      foreach ($hidden_columns as $column_id) {
          echo "Hidden column: " . $column_id . "<br/>";
      }
      

    Example Code:

    $screen = get_current_screen();
    $hidden_columns = get_hidden_columns($screen);
    
    echo "Hidden Columns in '{$screen->id}': <br/>";
    foreach ($hidden_columns as $column_id) {
        echo "- " . $column_id . "<br/>";
    }
    

    Conclusion:

    This function is handy for developers who need to customize the WordPress admin interface, particularly when dealing with the visibility of columns on various admin screens. It allows you to programmatically determine which columns are hidden, enabling more dynamic and user-friendly customizations.

Leave an answer