Last active
March 25, 2023 00:31
Star
You must be signed in to star a gist
Handy Links Widget for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Handy Links Widget for WordPress | |
Plugin URI: https://www.karks.com | |
Description: Quick links to various website functionality and removal of some Dashboard clutter. | |
Version: 1.0 | |
Author: Eric Karkovack | |
Author URI: https://www.karks.com | |
Code culled from various sources, including the WordPress Codex. | |
*/ | |
function handylinks_add_dashboard_widgets() { | |
// Only Admins and Editors Should See this. | |
if ( ( current_user_can( 'activate_plugins' ) ) || current_user_can('delete_pages') ){ | |
wp_add_dashboard_widget( 'handy_dashboard_widget', 'Handy Links', 'handy_dashboard_widget_function' ); | |
} // end of Admin check. | |
// 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 dashboard widget from the end of the array | |
$csf_widget_backup = array( 'handy_dashboard_widget' => $normal_dashboard['handy_dashboard_widget'] ); | |
unset( $normal_dashboard['handy_dashboard_widget'] ); | |
// Merge the two arrays together so our widget is at the beginning | |
$sorted_dashboard = array_merge( $csf_widget_backup, $normal_dashboard ); | |
// Save the sorted array back into the original metaboxes | |
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard; | |
} | |
add_action( 'wp_dashboard_setup', 'handylinks_add_dashboard_widgets' ); | |
/* | |
THE WIDGET | |
Now, let's build the widget. You can add or remove links as you wish. | |
Each link uses a Dashicon, which you can find at https://developer.wordpress.org/resource/dashicons/ | |
*/ | |
function handy_dashboard_widget_function() { | |
echo '<h2>Handy Links</h2>'; | |
echo '<ul class="handy-widget">'; | |
echo '<li><span class="dashicons dashicons-admin-site"></span> <a href="' . esc_url( home_url() ).'" target="_blank">Visit the Website</a></li>'; // The front end of the website. | |
echo '<li><span class="dashicons dashicons-admin-page"></span> <a href="' . esc_url( admin_url() ).'edit.php?post_type=page">Site Pages</a></li>'; // Pages. | |
echo '<li><span class="dashicons dashicons-admin-post"></span> <a href="' . esc_url( admin_url() ).'edit.php">Blog Posts</a></li>'; // Posts. | |
// echo '<li><span class="dashicons dashicons-admin-post"></span> <a href="' . esc_url( admin_url() ).'custom-link.php">Your Custom Link Goes Here</a></li>'; // Custom Link. | |
echo '</ul>'; | |
} // end of function | |
// Now, let's remove the default WordPress widgets so they don't get in our way. | |
function remove_dashboard_meta() { | |
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' ); | |
remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' ); | |
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' ); | |
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8 | |
} | |
add_action( 'admin_init', 'remove_dashboard_meta' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment