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

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

Fires while instantiating a third-party widget for the Dashboard and Analytics 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 VAPStatisticsWidget 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('vikappointments_fetch_statistics_widget_classname', function($classname, $widget)
{
    // define list of supported widgets
    $supported = array(
        'foo_test',
    );

    if (in_array($widget, $supported))
    {
        // include widget class handler
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR . $widget . '.php';
	
        // convert widget name in Pascal Case: "foo_test" becomes "FooTest"
        $widget = str_replace(' ', '', ucwords(str_replace('_', ' ', $widget)));

        // set widget class name (e.g. "VAPStatisticsWidgetFooTest")
        $classname = 'VAPStatisticsWidget' . $widget;
    }

    return $classname;
}, 10, 2);

Changelog

Version Description
1.2 Introduced.
Last Update: 2021-09-30
Helpful?

This website uses cookies

This website uses cookies and similar technologies. Some of these technologies are necessary for the proper functioning of the website (Essential). Other technologies are used to evaluate user behavior (Analysis), to integrate external media or for advertising purposes. If you agree, these technologies are activated. For details, please refer to the privacy policy. From this same page you can also update/revoke your consent. If you do not give your consent, only cookies with essential functions will be activated.

We use technologies from the USA on our website. If you give your consent, you therefore consent at the same time to your data being transferred to the USA. We would like to point out that the USA does not have a level of data protection comparable to that of the EU and that any US company can be obliged by state authorities to hand over your data without you having any effective legal protection against this.