Archives

Add Extra Content to the Widgets Admin

I don’t have a good example of why you might want or need this, but nevertheless, if you should want to add additional text to the Widgets admin, it’s pretty darn easy:

add_action( 'widgets_admin_page', 'widgets_admin_info' );
function widgets_admin_info() {
    ?>
    <p style="clear:both;">Hello there!!</p>
    <?php
}

Remove Unwanted Dashboard Widgets

If you feel your dashboard is too cluttered, you can use this to remove any or all of the widgets

add_action( 'wp_dashboard_setup', 'remove_wp_dashboard_widgets' );
function remove_wp_dashboard_widgets() {
	//Plugins
	wp_unregister_sidebar_widget( 'dashboard_plugins' );
	remove_meta_box('dashboard_plugins', 'dashboard', 'normal');

	//Right Now
	//wp_unregister_sidebar_widget( 'dashboard_right_now' );
	//remove_meta_box('dashboard_right_now', 'dashboard', 'normal');

	//Recent Comments
	//wp_unregister_sidebar_widget( 'dashboard_recent_comments' );
	//remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

	//Incoming Links
	//wp_unregister_sidebar_widget( 'dashboard_incoming_links' );
	//remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');

	//WordPress Blog
	wp_unregister_sidebar_widget( 'dashboard_primary' );
	remove_meta_box('dashboard_primary', 'dashboard', 'side');

	//Other WordPress News
	wp_unregister_sidebar_widget( 'dashboard_secondary' ); 
	remove_meta_box('dashboard_secondary', 'dashboard', 'side');

	//Quick Press
	//wp_unregister_sidebar_widget( 'dashboard_quick_press' ); 
	//remove_meta_box('dashboard_quick_press', 'dashboard', 'side');

	//Recent Drafts
	//wp_unregister_sidebar_widget( 'dashboard_recent_drafts' ); 
	//remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
}

Simple “Upcoming Posts” Widget

Here’s a simple widget for displaying upcoming posts. Title of widget and number of posts shown is configurable.

add_action( 'widgets_init', 'upcoming_posts_widget_load_widget' ); /* Add our function to the widgets_init hook. */
function upcoming_posts_widget_load_widget() { register_widget( 'upcoming_posts_widget' ); } /* Function that registers our widget. */

class upcoming_posts_widget extends WP_Widget {

    function upcoming_posts_widget() {
        $widget_ops = array( 'classname' => 'upcoming_posts', 'description' => 'Upcoming Posts Widget' ); /* Widget settings. */
        $control_ops = array(  'id_base' => 'upcoming_posts' ); /* Widget control settings. */
        $this->WP_Widget( 'upcoming_posts', 'Upcoming Posts Widget', $widget_ops, $control_ops ); /* Create the widget. */
    }

    function widget( $args, $instance ) {
        extract( $args );  /* before/after widget, before/after title (defined by themes). */
        extract($instance);

        echo $before_widget;
        echo $before_title . $title . $after_title; 

        echo '<ul>';
        $posts = get_posts("post_status=future&numberposts=$numberposts&order=ASC");
        $posts = array_reverse($posts);
        foreach ($posts as $p) {
            echo '<li>'. $p->post_title .'</li>';
        }
        echo '</ul>';
        echo $after_widget; 
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['numberposts'] = (int) $new_instance['numberposts'];
        return $instance;
    }

    function form( $instance ) {
        $defaults = array( 'title' => 'Upcoming Posts', 'numberposts' => 3 );
        $instance = wp_parse_args( (array) $instance, $defaults );
    ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">Number Posts:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" value="<?php echo $instance['numberposts']; ?>" />
            </select>
        </p>
    <?php
    }
}