0

I'm building a settings page of a plugin using the WP Settings API. So far so good, every fields are saved fine.

Finally I need 2 extra custom buttons (besides the original submit button) which should execute some special actions. In this case: "clear_cache" and "clear_image_cache" at the bottom of the code.

But unfortunately I cannot handle the click on these buttons. In my old implementation I were able to check the $_POST variable like:

if ( isset( $_POST[ 'clear_cache' ] ) && $_POST[ 'clear_cache' ] ) {

But this doesnt work any more. The $_POST variable is empty every time.

Any tipps how I can handle the custom button clicks within my code below?

Thanks in advance :)

<?php
if (!class_exists('Plugin_Settings')) {
    class Plugin_Settings
    {
        protected $parent = null;

        /**
         * Construct the plugin object
         */
        public function __construct($parent)
        {
            $this->parent = &$parent;
            $this->actions();
        }

        function actions () {
            add_action('admin_menu', array($this, 'add_admin_menu'));
            add_action('admin_init', array($this, 'settings_init'));
        }

        function add_admin_menu() {

            add_options_page(
                'plugin-settings',
                'plugin-settings',
                'manage_options',
                'plugin',
                array($this, 'options_page')
            );

        }

        /*
         * Setting Sections & Fields
         */
        function settings_init()
        {

            register_setting(
                'plugin_settings', // Settings page
                'plugin_settings', // Option name
                array($this, 'settings_callback') // callback
            );

            add_settings_section(
                'plugin_settings_section_configuration',
                __('Configuration', 'plugin'),
                array($this, 'settings_section_configuration_callback'),
                'plugin_settings'
            );

            // Styling
            add_settings_field(
                'plugin_box_style',
                __('Box Style', 'plugin'),
                array($this, 'box_style'),
                'plugin_settings',
                'plugin_settings_section_configuration'
            );
        }

        /*
         * Render & Callbacks
         */

        function box_style()
        {
            $box_style = isset($this->options['plugin_box_style']) ? $this->options['plugin_box_style'] : 'default';
            ?>
            <select name="plugin_settings[plugin_box_style]">
                <?php foreach ($this->available_styles as $value => $name) { ?>
                    <option value="<?php echo $value; ?>" <?php selected($box_style, $value); ?>><?php _e($name, 'plugin') ?></option>
                <?php } ?>
            </select>
        <?php

        }


        function settings_section_configuration_callback()
        {

            echo __('This section description', 'plugin');

        }

        function button_handler() {


            if ( isset( $_POST['clear_cache'] ) ) {
                echo 'clear_cache';
                // do clear cache
            }

            if ( isset( $_POST['clear_image_cache'] ) ) {
                echo 'clear_image_cache';
                // do clear image cache
            }
        }

        function settings_callback( $input ) {
            // Source: http://wpengineer.com/2139/adding-settings-to-an-existing-page-using-the-settings-api/ (Validation)
            return $input;
        }


        function options_page()
        {

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

                <?php $this->button_handler(); ?>

                <h2><?php _e('Plugin for WordPress', 'plugin') ?></h2>

                <?php
                // Source: http://code.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-7-validation-sanitisation-and-input-i--wp-25289 (Tabs)
                settings_fields('plugin_settings');
                do_settings_sections('plugin_settings');
                ?>

                <p class="submit">
                    <?php
                    // Actions
                    submit_button('', 'primary', 'save', false);
                    echo ' &nbsp; ';
                    submit_button(__('Clear Cache', 'plugin'), 'delete', 'clear_cache', false);
                    echo ' &nbsp; ';
                    submit_button(__('Clear Image Cache', 'plugin'), 'delete', 'clear_image_cache', false);
                    ?>
                </p>

            </form>
        <?php

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.