Making an options page for Wordpress Plugin


Previous posts Creating a Widget for Wordpress, Create a Simple Plugin for Wordpress and Adding Options to Wordpress Widget have given an introduction on how I have created a plugin for wordpress.  This final post will show how I added an options page.  The plugin adds php files to different areas and the options will set which files will be active.  This is based mainly on Handling Plugins Options in WordPress 2.8 with register_setting(), another similar method can be found at WordPress Settings API Tutorial which handles the input fields in a different way.

First we need to tell wordpress to create an options screen using


add_action('admin_menu', 'technixa_add_page');

function technixa_add_page() {

    add_options_page('Technixa options Page', 'Technixa Options', 'manage_options', 'technixa-options', 'technixa_options_page');

}

add_options_page creates a link to the page under settings in the admin pages, this can be changed to one of the following add_options_page, add_management_page (tools), add_pages_page, add_posts_page, add_comments_page, add_links_page, add_media_page, add_dashboard_page, add_object_page add_utility_page to change which menu it appears under.

The page created will be titled “Technixa Options Page” and it will be listed under the menu as Technixa options.  manage_options means only people with admin rights will see it available.  the link to the page will be /wp-admin/options-general.php?page=technixa-options and the contents of the page will be contained in the function technixa_options_page

We also need to call a function to register the settings to be stored in the database using


add_action('admin_init', 'function_technixa_options_init');


function function_technixa_options_init() { 

    register_setting('technixa_options', 'plugin_technixa_options', 'technixa_data_validate'); 

    }

This will register a set of options called technixa_options which will be stored in the database under plugin_technixa_options and will use a function called technixa_data_validate to validate that the input is correct

The next function needed is the actual content of the options page


<?php    function technixa_options_page() {
?> 

<div class="wrap">  <h2>Technixa options</h2>

<form method="post" action="options.php">

<?php settings_fields('technixa_options'); ?>

<?php $options = get_option('plugin_technixa_options'); ?>

<table class="form-table">

 

<tr valign="top"><th scope="row">Use Header Addition.</th>

<td><input name="plugin_technixa_options[useheaderfile]" type="checkbox" value="1" <?php checked('1', $options['useheaderfile']); ?> /></td>
</tr>

<tr valign="top"><th scope="row">Use footer Addition.</th>


<td><input name="plugin_technixa_options[usefooterfile]" type="checkbox" value="1" <?php checked('1', $options['usefooterfile']); ?> /></td>
</tr>

<tr valign="top"><th scope="row">Add before post.</th>
<td><input name="plugin_technixa_options[useprepostfile]" type="checkbox" value="1" <?php checked('1', $options['useprepostfile']); ?> /></td>
</tr>

<tr valign="top"><th scope="row">Add after posts.</th>
<td><input name="plugin_technixa_options[useafterpostfile]" type="checkbox" value="1" <?php checked('1', $options['useafterpostfile']); ?> /></td>
</tr>

</table>

<p class="submit">
 <input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?>" />
</p>
</form>


</div>
 

<?php } ?>

 

This uses php forms, more information on these can be found at tizag.com or w3schools.com.

The final function is the one to validate the input, here I used


function technixa_data_validate($input) {
        $input['usefooterfile']    = ($input['usefooterfile'] == 1 ? 1 : 0);

    $input['useheaderfile']    = ($input['useheaderfile'] == 1 ? 1 : 0);

    $input['useprepostfile']   = ($input['useprepostfile'] == 1 ? 1 : 0);

    $input['useafterpostfile'] = ($input['useafterpostfile'] == 1 ? 1 : 0);

    $input['widget_title']     = wp_filter_nohtml_kses($input['widget_title']);

    return $input;

}

now that the options are saved they can be retrieved by using get_option.  For example to choose whether to include the header addition in this plugin I use


add_action('wp_head', 'technixa_header');
    function technixa_header() {

    $options = get_option("plugin_technixa_options");

    if ($options['useheaderfile']) {

        include "phpfiles/header.php";

    }

}

One final thing i added was a settings link in the plugin screen beside the activation link as in wpengineer.com.


add_filter('plugin_action_links', 'technixa_settings_link', 10, 2); 

    function technixa_settings_link($links, $file) {
        static $this_plugin;

    if( !$this_plugin ) $this_plugin = plugin_basename(__FILE__);

    if( $file == $this_plugin ){

        $settings_link = '<a href="themes.php?page=plugin_technixa_options">' . __('Settings') . '</a>';

        $links = array_merge( array($settings_link), $links);

    }

    return $links;

}
Bookmark and Share

    Leave a Reply

    Powered by WP Hashcash