How to Create Widget Tools With PHP
You can create various Web elements using PHP, such as widgets, to make your website more efficient and interactive. Widgets refer to any icon or graphical interface element on your computer or the Internet that perform specific functions, such as report production or Web integration. With PHP, you can create different kinds of widgets for different functions and integrate them into your website to interact more with your site guests.
Instructions
-
-
1
Add the following function to your code to establish the controls for your Dashboard Widgets:
wp_add_dashboard_widget($widget_id, $widget_name, $callback, $control_callback = null)
To make it run, insert the following action code:
do_action( 'wp_dashboard_setup' );
-
2
Insert the following code to create your PHP widget that will greet your visitors when they visit your website:
// Create the function to output the contents of our Dashboard Widget
function example_dashboard_widget_function() {
// Display whatever it is you want to show
echo "Hello World, I'm a great Dashboard Widget";
}
// Create the function use in the action hook
function example_add_dashboard_widgets() {
wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
}
// Hook into the 'wp_dashboard_setup' action to register our other functions
add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );
-
-
3
Enter the following function that will manually alter the internal array of your site meta boxes and place your widget at the top of the list, so it will display before any other widget:
function example_add_dashboard_widgets() {
wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
// Globalize the metaboxes array, this holds all the widgets for wp-admin
global $wp_meta_boxes;
// Get the regular dashboard widgets array
// (which has our new widget already but at the end)
$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
// Backup and delete our new dashbaord widget from the end of the array
$example_widget_backup = array('example_dashboard_widget' => $normal_dashboard['example_dashboard_widget']);
unset($normal_dashboard['example_dashboard_widget']);
// Merge the two arrays together so our widget is at the beginning
$sorted_dashboard = array_merge($example_widget_backup, $normal_dashboard);
// Save the sorted array back into the original metaboxes
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
-
4
Insert the following function into your code to set your widget to display on the right side or column of your website:
// global the $wp_meta_boxes variable (this will allow us to alter the array)
global $wp_meta_boxes;
// Then we make a backup of your widget
$my_widget = $wp_meta_boxes['dashboard']['normal']['core']['{widget id here}'];
//We then unset that part of the array
unset($wp_meta_boxes['dashboard']['normal']['core']['{widget id here}']);
//Now we just add your widget back in
$wp_meta_boxes['dashboard']['side']['core']['{widget id here}'] = $my_widget;
-
1
Tips & Warnings
Always preview the appearance of your widgets before publishing them to prevent any overlapping issue. Learn as many PHP tutorials online as you can to become familiar with this application.
References
Resources
- Photo Credit Brendan Smialowski/Getty Images News/Getty Images