English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Dashboard  >  Fetch Widget Classname

apply_filters( 'vikrestaurants_fetch_statistics_widget_classname', string $classname, string $widget )

Fires while instantiating a third-party widget for the dashboard and statistics pages.


Description

The plugins MUST include here all the resources needed to the widget, otherwise it wouldn't be possible to instantiate the returned classes.

The event should return the PHP class name related to the requested widget.

In case the specified class doesn't exist, an exception will be thrown. The class MUST extend the VREStatisticsWidget abstract class.

NOTE: the file in which the widget class is located MUST be explicitly loaded.


Parameters

$classname

(string)  The classname that will be used to instantiate the widget object.

$widget

(string)  The name of the widget to include, which is equals to the base name of the file (without .php).


Example

The example provides the instantiation of the "test" widget located within the VikWP plugin.

/wp-content/plugins/vikwp/widgets/test.php
/**
 * Trigger filter to let the plugins include external widgets.
 * The plugins MUST include the resources needed, otherwise
 * it wouldn't be possible to instantiate the returned classes.
 *
 * @param  string  $classname  The classname of the widget.
 * @param  string  $widget     The name of the widget to include.
 */
add_filter('vikrestaurants_fetch_statistics_widget_classname', function($classname, $widget)
{
    // define list of supported widgets
    $supported = array(
        'test',
    );

    if (in_array($widget, $supported))
    {
        // include widget class handler
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR . $widget . '.php';

        // set widget class name
        $classname = 'VREStatisticsWidget' . ucfirst($widget);
    }

    return $classname;
}, 10, 2);

Changelog

Version Description
1.0 Introduced.
Last Update: 2021-01-24
Helpful?