Simple editing of plugin options in WortdPress



WordPress 1.2 has a simple built-in mechanism to generate an edit form with all options from one particular group. Unfortunately this has been removed in the CVS version. I hope that something similar will still be possible in the final 1.3 version…

Here is how I am using it in my current version of the PictPress plugin. This adds my own group of options if not already present:

$pp_group_id = $wpdb->get_var(\"SELECT group_id FROM $tableoptiongroups
                               WHERE group_name = 'PictPress'\");
if (!$pp_group_id) {
        $wpdb->query(
                \"INSERT INTO $tableoptiongroups
                 (group_name, group_desc)
                 VALUES
                 ('PictPress', 'Settings used by PictPress plugin.')\"
        );
        $pp_group_id = $wpdb->insert_id;
}

Then we have to keep a sequence number of the number of options that have been added to the group:

$pp_option_seq = 0;

And this function adds an option to my group:

function pp_add_option ($name, $type, $width, $value, $desc) {
        global $wpdb, $tableoptions, $tableoptiongroup_options,
               $pp_group_id, $pp_option_seq;
        $pp_option_seq++;
        $id = $wpdb->get_var(\"SELECT option_id FROM $tableoptions
                              WHERE option_name = '$name'\");
        if (!$id) {
                $wpdb->query(\"INSERT INTO $tableoptions
                              (option_name, option_value, option_description,
                               option_type, option_admin_level, option_width)
                              VALUES
                              ('$name', '$value', '$desc', $type, 8, $width)\"
                            );
                $id = $wpdb->insert_id;
                $wpdb->query(\"INSERT INTO $tableoptiongroup_options
                              (group_id, option_id, seq)
                              VALUES
                              ($pp_group_id, $id, $pp_option_seq)\"
                            );
        }
}

The edit form for this group of options is shown by going to the URL wp-admin/options.php?option_group_id=$pp_group_id. An entry for PictPress also automagically appears on all Options pages. Note that Link Manager was already using this mechanism. It would be a shame if such a low threshold edit form would disappear in 1.3…

Leave a Reply