DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.62k stars 2.25k forks source link

Error while adding Dashboard widget #551

Closed NeaMitika closed 3 years ago

NeaMitika commented 3 years ago

Hello,

First of all thank you for creating this boilerplate it helps a lot by having a headstart in developing a plugin.

I am kinda noob so be patient, I want to create a new dashboard widget and i have added the following code:

to admin/class-plugin-name-admin.php

function wporg_add_dashboard_widgets() {
        wp_add_dashboard_widget(
            'wporg_dashboard_widget',                          // Widget slug.
            esc_html__( 'Example Dashboard Widget', 'wporg' ), // Title.
            'wporg_dashboard_widget_render'                    // Display function.
        ); 
    }

function wporg_dashboard_widget_render() {
        // Display whatever you want to show.
        include_once 'partials/bacheca-admin-display.php';
    }

and to includes/class-plugin-name.php inside private function define_admin_hooks()

$this->loader->add_action( 'wp_dashboard_setup', $plugin_admin, 'wporg_add_dashboard_widgets' );

The widget itself is visible inside the Dashboard but there is no content inside, its white. So i have activated debugmode and i get this error

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'wporg_dashboard_widget_render' not found or invalid function name in /home/manoliui/wordpress.manoliu.it/wp-admin/includes/template.php on line 1389

NeaMitika commented 3 years ago

I have found the error while i was watching a video on YT,

Using the VS Code plugin "Wordpress Snippets" when i was tipying wp_add_dashboard_widget it gave me all the attributes that must go inside. For Example: wp_add_dashboard_widget( $widget_id:string, $widget_name:string, $callback:callable, $control_callback:callable|null, $callback_args:array|null )

What i was doing wrong in the main code was the "$callback:callable" that must be an array not a string.

The correct code is this one:

wp_add_dashboard_widget( 
    'wp-dashboard-luciano',             // Widget ID
    'La Mia Dashboard',                 //Widget Name
        array ( 
            $this, 
            'wporg_dashboard_widget_render' // The callback functions to display text
            ),
        );