Dashboard API
This article is a guide on how to add controls and extend the Cornerstone Dashboard.
Revamped in Cornerstone 7.7.0+, is our new dashboard. This dashboard is easily extendable, allowing you to create your own integrations.
Registering Data
Where you are storing your data is the first step to a dashboard integration. Cornerstone allows for quick access to the wp_options
table as well as completely custom which you will see in the Advanced
section.
DB Options
This option will use the wp_options
table. It also handles saving in the dashboard allowing you to easily . It currently only accepts one array key values
which is an associative array of your database options. You do not need to use update_options
, this function will handle the default values you pass to it.
cs_dashboard_register_options([
'values' => [
'my_option' => true,
],
]);
Advanced
This strategy is for integrations that don't use wp_options
and needs more granular control of the values and saving those values.
Registry values can be done through the filter cs_dashboard_values
.
add_filter('cs_dashboard_values', function($values) {
$values['my_option'] = true;
return $values;
});
Then saving can be done through the action cs_dashboard_save
. This will send back every registered value including the ones you have defined.
add_action('cs_dashboard_save', function($params) {
if (isset($params['my_option'])) {
file_put_contents('/tmp/my_option.txt', $params['my_option']);
}
});
Creating Controls
cs_dashboard_add_tab
cs_dashboard_add_tab
can be used to auto create a tab in the dashboard. It has the following arguments.
id
unique ID of the dashboard. This is used internally and not visually displayed.config
This will contain thelabel
and thecontrols
array. See our Element API for control types and other options for controls.position
The position the tab will be added to. It defaults to-1
and any negative value will place the tab at the very end of the dashboard
cs_dashboard_add_tab('my_tab', [
'label' => __('My Tab'),
'controls' => [
[
'key' => 'my_option',
'type' => 'toggle',
'label' => __('My Option'),
],
],
], 2);
Adding to the General Controls
The cs_dashboard_general_controls
filter can be used to add controls to the General tab of the dashboard.
add_filter('cs_dashboard_general_controls', function($controls) {
$controls[] = [
'key' => 'my_option',
'type' => 'toggle',
'label' => __('My Option'),
];
return $controls;
});
Advanced
The cs_dashboard_controls
filter can be used to alter and add to the Dashboard controls. Each array item in this array is a Dashboard tab.
add_filter('cs_dashboard_controls', function($controls) {
$controls[] = [
'value' => 'my_tab',
'label' => __('My Tab'),
'controls' => [
[
'key' => 'my_option',
'type' => 'toggle',
'label' => __('My Option'),
],
],
];
return $controls;
});
See something inaccurate? Let us know