This function adds a new widget to the administration dashboard
Adding Dashboard Widgets
Example 1
// Function that outputs the contents of the dashboard widget function dashboard_widget_function( $post, $callback_args ) { echo "Hello World, this is my first Dashboard Widget!"; } // Function used in the action hook function add_dashboard_widgets() { wp_add_dashboard_widget('dashboard_widget', 'Example Dashboard Widget', 'dashboard_widget_function'); } // Register the new dashboard widget with the 'wp_dashboard_setup' action add_action('wp_dashboard_setup', 'add_dashboard_widgets' );
Example 2
add_action( 'wp_dashboard_setup', 'prefix_add_dashboard_widget' ); function prefix_add_dashboard_widget() { wp_add_dashboard_widget( 'my_dashboard_widget', 'Featured Dashboard Page', 'prefix_dashboard_widget', 'prefix_dashboard_widget_handle' ); } function prefix_dashboard_widget() { # get saved data if( !$widget_options = get_option( 'my_dashboard_widget_options' ) ) $widget_options = array( ); # default output $output = sprintf( '<h2 style="text-align:right">%s</h2>', __( 'Please, configure the widget ☝' ) ); # check if saved data contains content $saved_feature_post = isset( $widget_options['feature_post'] ) ? $widget_options['feature_post'] : false; # custom content saved by control callback, modify output if( $saved_feature_post ) { $post = get_post( $saved_feature_post ); if( $post ) { $content = do_shortcode( html_entity_decode( $post->post_content ) ); $output = "<h2>{$post->post_title}</h2><p>{$content}</p>"; } } echo "<div class='feature_post_class_wrap'> <label style='background:#ccc;'>$output</label> </div> "; } function prefix_dashboard_widget_handle() { # get saved data if( !$widget_options = get_option( 'my_dashboard_widget_options' ) ) $widget_options = array( ); # process update if( 'POST' == $_SERVER['REQUEST_METHOD'] && isset( $_POST['my_dashboard_widget_options'] ) ) { # minor validation $widget_options['feature_post'] = absint( $_POST['my_dashboard_widget_options']['feature_post'] ); # save update update_option( 'my_dashboard_widget_options', $widget_options ); } # set defaults if( !isset( $widget_options['feature_post'] ) ) $widget_options['feature_post'] = ''; echo "<p><strong>Available Pages</strong></p> <div class='feature_post_class_wrap'> <label>Title</label>"; wp_dropdown_pages( array( 'post_type' => 'page', 'selected' => $widget_options['feature_post'], 'name' => 'my_dashboard_widget_options[feature_post]', 'id' => 'feature_post', 'show_option_none' => '- Select -' ) ); echo "</div>"; }
Note: If not showing then you need to permission the function on your theme to run this code:
function.php
do_action( 'wp_dashboard_setup' );
You can use the add_meta_box() function instead of wp_add_dashboard_widget. Simply specify ‘dashboard’ in place of the $post_type. For example:
add_meta_box('id', 'Dashboard Widget Title', 'dash_widget', 'dashboard', 'side', 'high');