Can't display dashboard widget content
-
I’m trying to display dashboard widget content and i get the message
Warning: call_user_func() expects parameter 1 to be a valid callback, function ‘couponsgen_create_dashboard_widget’ not found or invalid function name in C:\wamp\www\wpdev1\wp-admin\includes\template.php on line 1037
My code for creating the widget is
<?php class Couponsgen_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Couponsgen_Loader as all of the hooks are defined * in that particular class. * * The Couponsgen_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/couponsgen-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Couponsgen_Loader as all of the hooks are defined * in that particular class. * * The Couponsgen_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/couponsgen-admin.js', array( 'jquery' ), $this->version, false ); } // call function to create our dashboard widget public function couponsgen_add_dashboard_widget() { wp_add_dashboard_widget( 'couponsgen_dashboard_widget', 'Coupons Generator Dashboard Widget', 'couponsgen_create_dashboard_widget' ); } // function to display our dashboard widget content public function couponsgen_create_dashboard_widget() { echo "<p>Hello World! This is my couponsgen Dashboard Widget</p>"; } public function add_plugin_admin_menu() { add_options_page( 'Coupons Generator plugin Setup', 'Coupons Gen', 'manage_options', $this->plugin_name, array($this, 'display_plugin_setup_page')); } public function add_action_links( $links ) { $settings_link = array( '<a href="' . admin_url( 'options-general.php?page=' . $this->plugin_name ) . '">' . __('Settings', $this->plugin_name) . '</a>', ); return array_merge( $settings_link, $links ); } public function display_plugin_setup_page() { include_once( 'partials/couponsgen-admin-display.php' ); } }and for adding the action
<?php class Couponsgen { /** * The loader that's responsible for maintaining and registering all hooks that power * the plugin. * * @since 1.0.0 * @access protected * @var Couponsgen_Loader $loader Maintains and registers all hooks for the plugin. */ protected $loader; /** * The unique identifier of this plugin. * * @since 1.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * The current version of the plugin. * * @since 1.0.0 * @access protected * @var string $version The current version of the plugin. */ protected $version; /** * Define the core functionality of the plugin. * * Set the plugin name and the plugin version that can be used throughout the plugin. * Load the dependencies, define the locale, and set the hooks for the admin area and * the public-facing side of the site. * * @since 1.0.0 */ public function __construct() { $this->plugin_name = 'couponsgen'; $this->version = '1.0.0'; $this->load_dependencies(); $this->set_locale(); $this->define_admin_hooks(); $this->define_public_hooks(); } /** * Load the required dependencies for this plugin. * * Include the following files that make up the plugin: * * - Couponsgen_Loader. Orchestrates the hooks of the plugin. * - Couponsgen_i18n. Defines internationalization functionality. * - Couponsgen_Admin. Defines all hooks for the admin area. * - Couponsgen_Public. Defines all hooks for the public side of the site. * * Create an instance of the loader which will be used to register the hooks * with WordPress. * * @since 1.0.0 * @access private */ private function load_dependencies() { /** * The class responsible for orchestrating the actions and filters of the * core plugin. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-couponsgen-loader.php'; /** * The class responsible for defining internationalization functionality * of the plugin. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-couponsgen-i18n.php'; /** * The class responsible for defining all actions that occur in the admin area. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-couponsgen-admin.php'; /** * The class responsible for defining all actions that occur in the public-facing * side of the site. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-couponsgen-public.php'; $this->loader = new Couponsgen_Loader(); } /** * Define the locale for this plugin for internationalization. * * Uses the Couponsgen_i18n class in order to set the domain and to register the hook * with WordPress. * * @since 1.0.0 * @access private */ private function set_locale() { $plugin_i18n = new Couponsgen_i18n(); $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); } /** * Register all of the hooks related to the admin area functionality * of the plugin. * * @since 1.0.0 * @access private */ private function define_admin_hooks() { $plugin_admin = new Couponsgen_Admin( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); $this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' ); $this->loader->add_action('wp_dashboard_setup', $plugin_admin, 'couponsgen_add_dashboard_widget'); // Add Settings link to the plugin $plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . $this->plugin_name . '.php' ); $this->loader->add_filter( 'plugin_action_links_' . $plugin_basename, $plugin_admin, 'add_action_links' ); } /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @since 1.0.0 * @access private */ private function define_public_hooks() { require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-couponsgen-loader.php'; $plugin_public = new Couponsgen_Public( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); } /** * Run the loader to execute all of the hooks with WordPress. * * @since 1.0.0 */ public function run() { $this->loader->run(); } /** * The name of the plugin used to uniquely identify it within the context of * WordPress and to define internationalization functionality. * * @since 1.0.0 * @return string The name of the plugin. */ public function get_plugin_name() { return $this->plugin_name; } /** * The reference to the class that orchestrates the hooks with the plugin. * * @since 1.0.0 * @return Couponsgen_Loader Orchestrates the hooks of the plugin. */ public function get_loader() { return $this->loader; } /** * Retrieve the version number of the plugin. * * @since 1.0.0 * @return string The version number of the plugin. */ public function get_version() { return $this->version; } }Can someone please help?
-
Hi there!
This is what appears to be the problem:
expects parameter 1 to be a valid callback, function
couponsgen_create_dashboard_widgetnot found or invalid function nameBecause you are calling it within the class you could try using:
wp_add_dashboard_widget( 'couponsgen_dashboard_widget', 'Coupons Generator Dashboard Widget', array( __CLASS__, 'couponsgen_create_dashboard_widget' ) );Hope that helps.
Thank you very much for the quick response!
I also had to turn my function to static cause I got the message
Strict standards: call_user_func() expects parameter 1 to be a valid callback, non-static method
The final code for adding the widget is
public function couponsgen_add_dashboard_widget() { wp_add_dashboard_widget( 'couponsgen_dashboard_widget', 'Coupons Generator Dashboard Widget', array(__CLASS__,'couponsgen_create_dashboard_widget') ); }and for putting content inside the widget is
public static function couponsgen_create_dashboard_widget() { echo "<p>Hello World! This is my couponsgen Dashboard Widget</p>"; }Thank you very much for your help!
I was wondering if there is a way to do it without using static… I’m creating a new instanse of the class Coupogen_Admin but i think it’s a lot more work to do.
Ok. I got it. The only thing I had to do was use the object itself by refering to it ($this). So I changed the code and voila. No need for a static function.
public function couponsgen_add_dashboard_widget() { //$cg_widget = new Couponsgen_Admin(); //$cg_widget->couponsgen_create_dashboard_widget(); wp_add_dashboard_widget( 'couponsgen_dashboard_widget', 'Coupons Generator Dashboard Widget', array($this, 'couponsgen_create_dashboard_widget') ); }
The topic ‘Can't display dashboard widget content’ is closed to new replies.