I am building a custom wordpress plugin and from inside my plugin I would like to edit the backend wordpress dashboard widget welcome page. I would like to add a custom widget in the dashboard that will show details about my custom widget. I am registering the widget and the only this I can see is the title. The metaboxes or the html of the widget does not show in the dashboard only the title of the widget. Need help to show and the html that I add in my callback function. Anyone can help?
PS - I can do it working from my functions.php file but is there a way to make it work from a custom wordpress plugin?
Here is the code of my plugin.
function my_custom_dashboard_widgets() {
wp_add_dashboard_widget("custom_help_widget", "WordPress Tutorials", "custom_dashboard_help");
}
add_action("wp_dashboard_setup", "my_custom_dashboard_widgets");
function custom_dashboard_help() {
echo "<p>Welcome to your custom dashboard widget! Need help?></p>";
}
CodePudding user response:
How plugin index.php page will like -
<?php
/*
Plugin Name: Custom Dashboard Widget
Description: Something
Author: anonymous
Version: 1.0.0
Author URI: https://example.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Custom_Dashboard_Widgets {
public function __construct() {
$this->init();
}
public function init(){
add_action( "wp_dashboard_setup", array( $this, "my_custom_dashboard_widgets" ) );
}
public function my_custom_dashboard_widgets() {
wp_add_dashboard_widget( "custom_help_widget", "WordPress Tutorials", array( $this, "custom_dashboard_help" ) );
}
public function custom_dashboard_help() {
include_once("templates/dashboard.php");
// or show all inside
// echo "Welcome to your custom dashboard widget! Need help?";
}
}
$custom_dashboard_widgets = new Custom_Dashboard_Widgets();
How templates > dashboard.php files will like -
<?php
echo "Welcome to your custom dashboard widget! Need help?";
?>
<h1> This is dashboard </h1>
Plugin Directory be like -
|--templates
|-----dashboard.php
|--index.php