Which hook for processing plugin page form data?

Which hook should I use for processing form data from my plugin pages. I currently use "admin_init". Is that correct? Was this hook intended to be used this way?

Topic admin-init dashboard admin Wordpress

Category Web


if you're processing data submitted from a custom form, then I'd say the init hook is the best hook to use. See example 1.


For processing plugin options I assume that you created some kind of an options page?

Here's an example of creating a checkbox setting inside an options page, with validation and everything else. Just create a new "myplugin-admin.php" file and insert the code. There's only one setting: remove junk from wp's area. Then include the admin file inside your main plugin php file (e.g. myplugin.php) -> include "myplugin-admin.php";

<?php

// SETUP THE MENU
function myplugin_setup_menu(){
        add_options_page( 'Settings', 'MyPlugin', 'manage_options', __FILE__, 'myplugin_build_options_page' );
}

// BUILD OPTIONS PAGE
function myplugin_build_options_page(){ ?>

<div id="myplugin" class="wrap">

    <div id="icon-options-general" class="icon32"></div>

    <h2 class="options-title">MyPlugin <?php _e("Settings"); ?></h2>

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

        <div id="poststuff">
            <div id="post-body" class="metabox-holder columns-2">

                <!-- main content -->
                <div id="post-body-content">
                    <div class="meta-box-sortables ui-sortable">
                        <div class="postbox">
                            <h3 class="hndle"><span><?php _e("General"); ?> <?php _e("Settings"); ?></span></h3>
                            <div class="inside">

                                <?php settings_fields('myplugin_options'); ?>
                                <?php do_settings_sections(__FILE__); ?>

                            </div><!-- .inside -->
                        </div><!-- .postbox -->
                    </div><!-- .meta-box-sortables .ui-sortable -->
                </div><!-- post-body-content -->

                <!-- sidebar -->
                <div id="postbox-container-1" class="postbox-container">
                    <div class="meta-box-sortables">
                        <div class="postbox">

                            <div class="inside">
                                <p><?php _e("Edit MyPLugin Settings"); ?></p>
                            </div><!-- .inside -->

                        </div><!-- .postbox -->
                    </div><!-- .meta-box-sortables -->
                </div><!-- #postbox-container-1 .postbox-container -->
            </div><!-- #post-body .metabox-holder .columns-2 -->
            <br class="clear">

            <?php myplugin_submit(); ?>

        </div><!-- #poststuff -->

    </form><!-- /.options-form -->

</div><!-- .wrap -->

<?php }

/* ================================================================ *\
    BUILD AND REGISTER OPTIONS
\* ================================================================

register_setting( $option_group, $option_name, $sanitize_callback );
    $option_group: a string representing the name of the settings group
    $option_name: the name of the option
    $sanitize_callback: a callback function that can handle any specific operations or sanitizing

add_settings_section( $id, $title, $callback, $page );
    $id: a unique ID for the section
    $title: a heading that will be displayed above the fields on the page
    $callback: can handle the creation of the section; if it’s declared, you must create the function, or an error will be thrown
    $page: defines the type of settings page that this section should be applied to; in our case, it should apply to our custom page, so we used __FILE__ as the name

add_settings_field( $id, $title, $callback, $page, $section, $args );
    $id: a unique ID for the field
    $title: the title before the field
    $callback: handles the displaying of the field
    $page: it should apply to our custom page, so we used __FILE__ as the name
    $section: set the section the the field belongs to
    $args: ka
*/
function myplugin_build_options () {
    $options = get_option('lmyplugin_options');
    if(!isset($options)) {
        //not present, so add
        $options = array(
            'remove_junk' => 1
        );
        add_option('myplugin_options', $options);
    }

    // Register Settings
    register_setting('myplugin_options', 'myplugin_options', 'myplugin_validate_settings');

    // Add Sections
    add_settings_section('myplugin_section_functions', __("Functions", "myplugin"), 'myplugin_section_functions_cb', __FILE__);

    /*
        ADD FIELDS TO SECTION
    */

    // Remove Junk
    add_settings_field('remove_junk', __('Remove Junk from Head', 'myplugin'), 'myplugin_setting_remove_junk', __FILE__, 'myplugin_section_functions');
}

// VALIDATE SETTINGS
function myplugin_validate_settings($options) {
    return $options;
}

// SETTINGS SECTION CLLBACK
function myplugin_section_functions_cb() {}

/* ================================================================ *\
    BUILD SETTINGS FIELDS
\* ================================================================ */

// Remove Junk
function myplugin_setting_remove_junk() {
    $options = get_option('myplugin_options'); ?>
    <fieldset>
        <legend class="screen-reader-text"><span><?php _e("Remove Junk from <head>", 'myplugin'); ?></span></legend>
        <label for="remove_junk">
            <input name="myplugin_options[remove_junk]" type="checkbox" id="remove_junk" value="1" <?php checked($options['remove_junk'], 1, true); ?> />
            <span><?php _e( 'Remove junk information from wordpress <head> area.', 'myplugin' ); ?></span>
        </label>
    </fieldset>
<?php }

/* ================================================================ *\
    MISC
\* ================================================================ */

// SUBMIT BUTTON
function myplugin_submit() { ?>
    <p class="submit"><input name="Submit" type="submit" class="button button-primary" value="<?php _e('Save Changes'); ?>" /><!-- /#.button-primary --></p><!-- /.submit -->
<?php }

// ADD STYLESHEET
function myplugin_register_head() {
    $url = plugins_url('css/admin-style.css', __FILE__ );
    echo '<link rel="stylesheet" href="'.$url.'" />';
}

// ADD ACTIONS
add_action('admin_head', 'myplugin_register_head');
add_action('admin_init', 'myplugin_build_options');
add_action('admin_menu', 'myplugin_setup_menu');


?>

Have fun trying that out and I hope I could help you.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.