Posts tagged ‘widget’

Adding Options to Wordpress Widget

In two previous Posts (Create a Widget for Wordpress and Create a Simple Plugin for Wordpress) I showed how I created a plug in to add content from files into the displayed theme.  Now I am going to also add an options page to the admin panel and an option to the widget for setting the displayed title.

First I’ll show how I added the option of changing the Title of the widget.  This was done following a post on Lonewolf Designs.

Previously I activated the widget using

<?php
add_action("plugins_loaded", "init_technixa");

function init_technixa() {
     register_sidebar_widget("Technixa", "technixa_widget");
}
?>

Now we also need to activate a control page on the widget by adding a line to make it

<?php

function init_technixa() {
    register_sidebar_widget("Technixa", "technixa_widget");
    register_widget_control('Technixa', 'technixa_widget_control');

}

?>

The extra line register_widget_control(’Technixa’, ‘technixa_widget_control’); registers the control panel which will be contained within the function called technixa_widget_control.

<?php
function technixa_widget_control() {

    $options = get_option("plugin_technixa_options"); 

    if ($_POST['technixa-Submit']) {
        $options['widget_title'] = htmlspecialchars($_POST['technixa-WidgetTitle']);
        update_option("plugin_technixa_options", $options);
    }

// html to show input box

?>

<p>
<label for="technixa-WidgetTitle">Widget Title: </label>
<input type="text" id="technixa-WidgetTitle" name="technixa-WidgetTitle" value="<?php echo $options['widget_title']; ?>" />

<input type="hidden" id="technixa-Submit"  name="technixa-Submit" value="1" />
</p>

<?php

}
?>

Here $options = get_option(”plugin_technixa_options”); will get any previously saved configuration options from the wordpress database.

Update option is what will store the content of $options in the wordpress database under plugin_technixa_options.  These saved options can be called upon using get_options.

For more on this try the following links

Create a Wordpress Widget From Scratch.

Writing a WordPress Plugin using PHP (PDF download from here)