Skip to content

Fix output formats for the theme mod get command #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

philipjohn
Copy link

Fixes #96

I maintained but improved the space-indented formatting for the table format as folks might be using this (as per @schlessera's comment) and then fixed the other formats by using dot notation to represent each field, given the formatter needs arrays with key and value.

For the table format, I opted to remove the => for the value to represent an array. I felt it was harder to read than simply leaving the value blank and letting the indent do the visual work.

For other formats I stripped out anything without a value.

For all formats booleans and empty arrays are represented by a string (e.g. [empty array]. There may be a better way to represent these, and I did consider var_export() but others may have better ideas.

Here are some examples of the new output:

$ wp eval 'print_r(get_theme_mods());'
Array
(
    [0] => 
    [nav_menu_locations] => Array
        (
        )

    [sidebars_widgets] => Array
        (
            [time] => 1749114163
            [data] => Array
                (
                    [wp_inactive_widgets] => Array
                        (
                            [0] => block-2
                            [1] => block-3
                            [2] => block-4
                            [3] => block-5
                            [4] => block-6
                        )

                    [sidebar-1] => Array
                        (
                            [0] => block-7
                        )

                )

        )

)
$ wp theme mod get --all --format=table
+-------------------------+---------------+
| key                     | value         |
+-------------------------+---------------+
| 0                       | [false]       |
| nav_menu_locations      | [empty array] |
| sidebars_widgets        |               |
|   time                  | 1749114163    |
|   data                  |               |
|     wp_inactive_widgets |               |
|       0                 | block-2       |
|       1                 | block-3       |
|       2                 | block-4       |
|       3                 | block-5       |
|       4                 | block-6       |
|     sidebar-1           |               |
|       0                 | block-7       |
+-------------------------+---------------+
$ wp theme mod get --all --format=csv
key,value
0,[false]
nav_menu_locations,"[empty array]"
sidebars_widgets.time,1749114163
sidebars_widgets.data.wp_inactive_widgets.0,block-2
sidebars_widgets.data.wp_inactive_widgets.1,block-3
sidebars_widgets.data.wp_inactive_widgets.2,block-4
sidebars_widgets.data.wp_inactive_widgets.3,block-5
sidebars_widgets.data.wp_inactive_widgets.4,block-6
sidebars_widgets.data.sidebar-1.0,block-7
$ wp theme mod get --all --format=yaml
---
- 
  key: "0"
  value: '[false]'
- 
  key: nav_menu_locations
  value: '[empty array]'
- 
  key: sidebars_widgets.time
  value: 1749114163
- 
  key: sidebars_widgets.data.wp_inactive_widgets.0
  value: block-2
- 
  key: sidebars_widgets.data.wp_inactive_widgets.1
  value: block-3
- 
  key: sidebars_widgets.data.wp_inactive_widgets.2
  value: block-4
- 
  key: sidebars_widgets.data.wp_inactive_widgets.3
  value: block-5
- 
  key: sidebars_widgets.data.wp_inactive_widgets.4
  value: block-6
- 
  key: sidebars_widgets.data.sidebar-1.0
  value: block-7
$ wp theme mod get --all --format=json | jq .
[
  {
    "key": "0",
    "value": "[false]"
  },
  {
    "key": "nav_menu_locations",
    "value": "[empty array]"
  },
  {
    "key": "sidebars_widgets.time",
    "value": 1749114163
  },
  {
    "key": "sidebars_widgets.data.wp_inactive_widgets.0",
    "value": "block-2"
  },
  {
    "key": "sidebars_widgets.data.wp_inactive_widgets.1",
    "value": "block-3"
  },
  {
    "key": "sidebars_widgets.data.wp_inactive_widgets.2",
    "value": "block-4"
  },
  {
    "key": "sidebars_widgets.data.wp_inactive_widgets.3",
    "value": "block-5"
  },
  {
    "key": "sidebars_widgets.data.wp_inactive_widgets.4",
    "value": "block-6"
  },
  {
    "key": "sidebars_widgets.data.sidebar-1.0",
    "value": "block-7"
  }
]

@philipjohn philipjohn requested a review from a team as a code owner June 5, 2025 14:04
Copy link
Member

@schlessera schlessera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only minor nitpicks for the code. Also, it would be good to have at least 1 feature test covering this.

@philipjohn philipjohn requested a review from schlessera June 6, 2025 08:14
@philipjohn
Copy link
Author

Oh, I will do tests by the way! Just probably not until after this week :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wp theme mod get --all produces malformed output for non-tabular formats
2 participants