Simplified Source

Code Clarity (TM) offers handcrafted and elegant Small Business Websites and Web Applications. We ar Web Development and Design firm based out of Boulder, Colorado. We specialize in W3C Standard complaint websites, SEO Marketing, PHP, Ruby on Rails, and much more. We also contribute to many open-source projects with interest and passion for Linux and Android.

(This blog post originated from a post that I made earlier on Forrst.)

Wordpress 3.x has a lot to offer us Developers. We should take advantage of it. I’ve been developing a lot of custom plugins and custom widgets for my Clients who use Wordpress as their CMS. Now there are a ton of tutorials and code snippets of how to create a custom Plugin or a custom Widget for your Wordpress Installation but I kept running into issues with my Widgets not being able to be reused. So, I created a basic template for a Custom Plugin that creates a Widget Class that can be reused and is more than customizable.

View on PasteBin  View on Forrst

Every Wordpress Developer has their own methods and own opinions on how to create Widgets and Plugins and I just wanted to share my method. This is my basic template which creates a Plugin for a Client, and wrapped in this Plugin is a custom Widget that shoots back some text. Of course you can get nice and complex and do what ever you want here. (My code is a compilation of research and publicly available information).

Constructive criticism and feedback is welcome, as always.

<?php
/*
Plugin Name: ClientName Custom Widget
Description: Displays a custom feature for your Client in the form of a Widget.
*/


class customClientWidget extends WP_Widget {

   
function custom_clientWidget() {
       
// Declare your Widget name, the class name and a Description for the Dashboard
        $widget_ops
= array('classname' => 'customClientWidget', 'description' => 'Displays something that you specify in the code below.' );
        $this
->WP_Widget('customClientWidget', 'Your Custom Widget', $widget_ops);
   
}

   
function widget( $args, $instance ) {
       
// Very basic code that takes the Title from the form function and then displays your code
        extract
($args, EXTR_SKIP);

        echo $before_widget
;
        $title
= empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

       
if (!empty($title))
          echo $before_title
. $title . $after_title;

       
// Content to be displayed on page goes here
        echo
'Displaying some data here.';
        runThisFunctionNow
();

        echo $after_widget
;

   
}

   
function update( $new_instance, $old_instance ) {
       
// Allows for the Widget to be reused
        $instance
= $old_instance;
        $instance
['title'] = $new_instance['title'];
       
return $instance;
   
}

   
function form( $instance ) {
       
// Creates a very simple form so that you can Title your Widget.
        $instance
= wp_parse_args( (array) $instance, array( 'title' => '' ) );
            $title
= $instance['title'];
       
?>
         
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
       
<?php
   
}

   
} // End of Widget Class

   
function customClient_widgets() {
   
// Register the Widget
    register_widget
( 'notable_pressWidget' );
   
}
   
// Quick WP Action to register the Widget
    add_action
( 'widgets_init', 'customClient_widgets' );

// From here you can upload the file and directory to your /wp-content/plugins/ directory, then goto Admin Dashboard and Plugins and Activate the Plugin. Afterwords you can goto your Widgets and add the Widget to a Sidebar. Simple stuff.


?>

When it comes down to it, that’s a pretty easy setup.