Skip to content

WP‐CLI Commands

William Patton edited this page Jul 30, 2024 · 5 revisions

The plugin includes a few handy WP-CLI commands inside of it for getting access to the Accessibility Checker stats about posts or the site.

There are currently 3 commands. All that commands are subcommands of accessibility-checker root.

  • wp accessibility-checker get-stats {{post_id}}
  • wp accessibility-checker delete-stats {{post_id}}
  • wp accessibility-checker get-site-stats

The get-stats command

This command is used to get the Accessibility Checker stats about a specific post/page. If the post_id passed exists and has stats then they will be returned as a JSON-formatted string of results.

wp accessibility-checker get-stats 911 --stat='errors,warnings'

Screenshot from 2024-07-17 13-16-11

Filtering the results with --stat

You can filter the returned results by any of the valid keys exposed in the command. Pass them as a list the flag, for example to get errors and warnings only use --stat="errors,warnings".

wp accessibility-checker get-stats 911 --stat='errors,warnings'

Screenshot from 2024-07-17 13-17-26

The delete-stats command.

This command is used to delete the Accessibility Checker stats about a specific post/page. If the post_id passed exist then it will clear the stats from it. Once stats are deleted they can't be restored, you have to scan the post again to generate new scan results.

wp accessibility-checker delete-stats 911

Screenshot from 2024-07-17 13-22-07

The get-site-stats command.

This command gets the Accessibility Checker stats for the entire site. It returns a JSON-formatted string. This data is not directly exposed in the plugin UI at the moment so it may be the most useful command for site owners or maintainers since it collates and displays data not otherwise visible as a group.

wp accessibility-checker get-site-stats

Screenshot from 2024-07-17 15-30-44

Filtering the result with --stat

You can filter the results to show only the keys you are interested in with the --stat command.

wp accessibility-checker get-site-stats --stat="cached_at_formatted,distinct_errors,distinct_warnings,contrast_errors"

Screenshot from 2024-07-17 15-32-49

Ensuring you get the latest stats after changes with --clear-cache

If you make changes to the site and need to check the latest numbers there may be times where you see cached data rather than latest. The command returns a value to show the last cache time and you can clear the cache with the --clear-cache flag, noticing the time change.

wp accessibility-checker get-site-stats --stat="cached_at_formatted,distinct_errors,distinct_warnings,contrast_errors" --clear-cache

Screenshot from 2024-07-17 15-32-58

Parsing the stats programatically.

If you need to integrate stats to your dashboard in some way then you can programatically parse the command results.

Using PHP you can pass the output through code like this to get a PHP array where $stats is the output captured from running the command:

$stats_array = json_decode(
	html_entity_decode(
		str_replace( 'Success: ', '', $stats )
	),
	true
);