Archive for July, 2009

Plugins and Widgets and WordPress–Oh my!

From my last post you know I’m trying to figure out the best configuration of my domain and subdomains.  I’m leaning toward the blog as my principal domain content with the photography blog as a linked subdomain.

sidebar

To sleep well at night with this change, I need a good way to interface my photography with the blog.  After testing a few options, I decided to try developing a WordPress Widget to display the most recent photo.  It was surprisingly easy!  I thought I might document a bit about the design of a WordPress widget.  I know my audience (consisting mostly of family) has really been hoping for a technical article with some code to sink their teeth into.  Right?

To really understand widgets, you should play around with them first in your own admin section, under Appearance->Widgets.  You can drag and drop from available widgets to the sidebar or footer to define the contents of those areas of your blog. You’ll notice when you drag a widget, a form appears with some fields to define.

On to creating a widget.  First, make a directory to hold all the plugin files in the wp-content/plugins directory.  Mine, for example, is wp-content/plugins/skphoto/.

Second, you need to define a class that extends WP_Widget, and includes a constructor function, widget function, update function, and form function.  This same file should also register the widget.  Here’s an outline of what that looks like:

class WidgetName extends WP_Widget
{
  /* constructor */
  function WidgetName() {}

  /* display the widget */
  function widget($args, $instance) {}

  /* save widget settings */
  function update($new_instance, $old_instance) {}

  /* edit form for the widget */
  function form($instance) {}
}

function WidgetNameInit()
{
  register_widget('WidgetName');
}
add_action('widgets_init', 'WidgetNameInit');

See Jesse Altman’s tutorial post for details on these functions.

I do want to give a bit more information about the widget function. If you want to access your MySQL database from within the widget function, you’ll need to add global $wpdb; to the top of your function.  Then, you can use standard WordPress database functions to pull data out.

Here’s the basics of my widget function for reference:

  function widget($args, $instance){
    global $wpdb;
    extract($args);
    $title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title']);
    $feed_url = empty($instance['feed_url']) ? 'http://photography.spencerkellis.net/atom.php' : $instance['feed_url'];

    # Before the widget
    echo $before_widget;

    # The title
    if ( $title )
    echo $before_title . $title . $after_title;

    $sql = "SELECT `path` FROM `photos` ORDER BY `date` DESC LIMIT 1";
    $path = $wpdb->get_var($sql);
    echo "";

    # After the widget
    echo $after_widget;
  }

Finally, activate your new plugin in the WordPress Plugins section. Then go back to the Widgets where the new widget will be available to drag and drop.

So there it is.  Easy-peezy, right?  Just a few lines of code, as I always like to say.  Here are a few resources if you want to explore on your own:

So I’ll be looking for your thoughts.  What do you think about this solution for a home page?

, , ,

No Comments

Joining Prodigious in the Whirl of Identity Crisis

Here’s the deal. I’ve got four websites right now: www.spencerkellis.net, blog.spencerkellis.net, gallery.spencerkellis.net, and photography.spencerkellis.net. That’s really just too many websites for one person–I mean, who are we kidding here?  How many ways are there to upload photos to a website? A thousand spencer’s on a thousand typewriters… How many PhD students does it take to… and the list goes on.

Is this narcissism at its finest or what?

I like and plan to keep the blog and the photography site (photograpy.spencerkellis.net). What do you all think about the gallery and the www.spencerkellis.net? And don’t worry about my feelings, fragile though they may be. It’s not like I spent 5 years of my life working on www.spencerkellis.net.

BTW, I’ve been making an attempt to post photos more regularly to the photography website.  The last 6 are from our vacation to Oregon if you want to check them out.

,

3 Comments

From the Sourthern Coast of Oregon

Emily and I have been touring the lighthouses and harbors along Highway 101 from Florence (OR) down to the Redwoods.  The whole southern coast of Oregon so far has been gorgeous.  I’m racking up the pictures with a few adventures along the way.  Most recent was the (temporary) death of our car battery at Cape Blanco, which I discovered at sundown after photographing the lighthouse against the sunset.  You can’t imagine a more dark and desolate (and beautiful) setting with just a lighthouse for company.  Luckily, I joined AAA literally 5 minutes before we left so we got a jump for free.

And I beat a challenge or two in Peggle on my iPod.

I have uploaded a few teasers, some of my favorites so far, to give you a taste of what we’re enjoying.  The pictures below are from Lakeside (first two), Newport (next two), and Cape Blanco (last one).  Lots more to come!

Classic Car Detail

Classic Car Detail

Night Lights on the Lake

Night Lights on the Lake (4th of July)

Monsieur Jellyfish

Monsieur Jellyfish

Newport Harbor

Newport Harbor

Cape Blanco Lighthouse

Cape Blanco Lighthouse

, , , , , ,

3 Comments