English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Dashboard  >  Fetch Supported Widgets

apply_filters( 'vikrestaurants_fetch_supported_statistics_widgets', mixed $widgets )

Fires while loading the supported widgets to display within the dashboard and statistics pages.


Description

This filter can be used to support additional types of widgets without having to edit any core files.

Here you should simply return the list of widgets that can be loaded within the script. Only the base name of the files have to be returned. The base name of the files to load can specify only letters, numbers and underscores.

Note: at the first execution of the hook the $widgets argument isn't an array.


Parameters

$widgets

(array|null)  The array holding the name of the external widgets to load.


Example

The example below adds support for all the widgets contained in a specific folder of a third-party plugin. Such as:

/wp-content/plugins/vikwp/widgets/

All the PHP files contained within the widgets folder of the VikWP plugin will be loaded.

/**
 * Trigger filter can be used to register external widgets to
 * extend the statistics dashboard without editing any core files.
 *
 * @param  array|null  $widgets  The array holding the widgets to load.
 */
add_filter('vikrestaurants_fetch_supported_statistics_widgets', function($widgets)
{
    if (!$widgets)
    {
        // first cycle of the filter, init the list of widgets
        $widgets = array();
    }

    // create base path to "widgets" folder contained within the plugin
    $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR;

    // load all PHP files from "widgets" folder
    $files = glob($base . '*.php');

    // IMPORTANT: map the widgets to return only the base name
    $files = array_map(function($widget)
    {
        return basename($widget);
    }, $files);

    // merge existing widgets with this ones
    return array_merge($widgets, $files);
});

Changelog

Version Description
1.0 Introduced.
Last Update: 2021-03-03
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information