Walkthrough for Week 2 Assignment

I will quickly go into how Assignment 2 could be solved.

For the first two items in the assignment (the custom login form and custom admin panel) you could use one of the many available plugins, see [1] and [2] in the resources section for my recommendations.

For the third item in the assignment (the custom widget) almost everyone in the class used a poll plugin. This was not what I wanted initially but reading the assignment now I can clearly see I led everyone in the wrong direction :)

The goal was to have a widget where in the admin you could choose your tribes and in the frontend you display them. Here is how the front view and the admin view could look like:

The Widget Barebones

Our widget needs an initial skeleton from where we can built upon. A widget can be created either through a plugin or a theme. In this case we will create a simple plugin that creates our widget. So to start you would create a directory ‘week2assign’ in the plugins directory and have a single file also called ‘week2assign.php’ containing:

This would be enough to have it in the plugins list and enable/disable it.

The Basic Widget

For a widget you normally need two functions, one to register the widget (register_sidebar_widget) and another to register the control function for the widget (register_widget_control). The first function has a basic template which you can see below and that is normally used:

This should be enough to start seeing your widget in Appearance->Widgets section.

The Widget Control

The widget control function is where you display the code for the widget controlling options. In this case it will be a checkbox for each of the possible options:

Because the list of checkboxes checked is kept into an array we need to use the serialize function in order to be able to save the array into the database.

A Small Enhancement

When you click save in the widget options you’ve probably noticed that the tribes you selected are not checked as marked. This is because although they are saved in the database, we didn’t check if they were marked. For that we can make a small enhancement to the control function to do just that:

You’ll notice now there is a is_tribe_selected function which is merely a function to check if a certain tribe is listed as checked. The code for this function is:

The Full Widget

The code for the full widget can be found here or downloaded here.

I hope this has been a useful walkthrough. Cheers and always Enjoy!

Resources

  • http://wordpress.org/extend/plugins/custom-login/
  • http://wordpress.org/extend/plugins/ozh-admin-drop-down-menu/

5 thoughts on “Walkthrough for Week 2 Assignment

  1. Matej,

    Thanks for the link, I find the WP_Widget class more complicated to use but it's probably the right way of doing things when it comes to writing widgets that will be released to the repository.

    Nuno

  2. I took a completely different approach, I modified the css of the wp-login webpage with an action and installed the wp-polls plugin to make (guess) the poll :P.

  3. Hey Nuno – not sure if you were aware of this but the script examples in your post don't render if pastebin.com is down (which it is right now). Any chance you could screengrab the code or include it as part of the text of the article in future please?

    Thanks!

Comments are closed.