0

I would like to add a button on the admin page to clear the transients I use.

See my code below:

<?php
// If this file is called directly, abort.
defined('ABSPATH') or exit();

/*
 * Generate the admin page
 */
function advanza_direct_plugin_settings() {
    $form = new advanza_direct_form;
    $plugin_settings = new advanza_direct_plugin;
    // For debugging
    // $form->debug();
    // $plugin_settings->debug();
    ?>
    <div class="wrap">
        <img src="<?php echo plugin_dir_url( __FILE__ ) . '/assets/img/advanza-direct.png'; ?>" id="advanza-direct-logo">
        <h1><?php echo __('Advanza Direct form', 'advanza_form_shortcode');?></h1>
        <p>Advanza Direct form settings, enter your specific settings below:</p>

    <?php if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true'):
        echo '<div id="setting-error-settings_updated" class="updated settings-error">
        <p><strong>' . __("Settings saved", "advanza_form_shortcode") . '</strong></p>
     </div>';
    endif;
    ?>
    <form method="post" action="options.php">
        <?php settings_fields('advanza_direct_plugin_settings'); ?>
        <?php do_settings_sections('advanza_direct_plugin_settings'); ?>
        <table id="advanza-direct-form-settings" class="form-table form-settings">
            <tr valign="top">
                <th scope="row"><?php echo __('Form to:', 'advanza_direct_form_to');?></th>
                <td>
                    <input type="text" name="advanza_direct_form_to" value="<?php echo get_option('advanza_direct_form_to'); ?>" placeholder="[email protected]">
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php echo __('Form subject:', 'advanza_direct_form_subject');?></th>
                <td>
                    <input type="text" name="advanza_direct_form_subject" value="<?php echo $form->get_subject; ?>" placeholder="Aanmelding professional"> - yourdomain.com (automatic)
                </td>
            </tr>
        </table>
        <?php submit_button(); ?>
        <h2>How to use the shortcode?</h2>
        <p>Use <input id="shortcode1" value="[advanza-direct-form]" size="18">
            <button class="shortcode" data-clipboard-target="#shortcode1">
                <img src="<?php echo plugins_url('/assets/img/clippy.svg', __FILE__);?>" width="14" alt="Copy to clipboard">
            </button> in your pages, posts and widgets. Use the shortcode whenever you want to use the Advanza form.<br>
        Or you could use the Advanza form widget functionality. To use the widget, simply go to:<strong> > Appearance > Widgets</strong></p>

        <br><br>

        <h2>Advanza Direct terms of service and privacy policy</h2>
        <table id="advanza-direct-plugin-settings" class="form-table form-settings">
            <tr valign="top">
                <th scope="row"><?php echo __('Terms of service URL:', 'advanza_direct_terms_of_service');?></th>
                <td>
                    <input type="text" name="advanza_direct_terms_of_service" value="<?php echo get_option('advanza_direct_terms_of_service'); ?>" placeholder="https://advanzadirect.nl/advanza-direct/terms-of-service.html">
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php echo __('Privacy policy URL:', 'advanza_direct_privacy_policy');?></th>
                <td>
                    <input type="text" name="advanza_direct_privacy_policy" value="<?php echo get_option('advanza_direct_privacy_policy'); ?>" placeholder="https://advanzadirect.nl/advanza-direct/privacy-policy.html">
                </td>
            </tr>
        </table>
        <?php submit_button(); ?>

        <h2>Shortcode for Advanza terms of service and privacy policy</h2>
        <p><strong>Terms of service shortcode:</strong> <input id="shortcode2" value="[advanza-direct-terms-of-service]" size="30">
            <button class="shortcode" data-clipboard-target="#shortcode2">
                <img src="<?php echo plugins_url('/assets/img/clippy.svg', __FILE__);?>" width="14" alt="Copy to clipboard">
            </button>
        </p>
        <p>
            <strong>Privacy policy shortcode:</strong> <input id="shortcode3" value="[advanza-direct-privacy-policy]" size="28">
                <button class="shortcode" data-clipboard-target="#shortcode3">
                    <img src="<?php echo plugins_url('/assets/img/clippy.svg', __FILE__);?>" width="14" alt="Copy to clipboard">
                </button>
        </p>
    </form>

    <script>
    jQuery(document).ready(function( $ ) {
        // For debugging
        // console.log('jQuery is ready!');

        $('.shortcode').click(function( event ) {
            event.preventDefault();
        });

        // Tooltip
        $('.shortcode').tooltip({
            trigger: 'click',
            placement: 'top'
        });

        function setTooltip(btn, message) {
            $(btn).tooltip('hide')
            .attr('data-original-title', message)
            .tooltip('show');
        }

        function hideTooltip(btn) {
            setTimeout(function() {
                $(btn).tooltip('hide');
            }, 500);
        }

        // Clipboard
        var clipboard = new ClipboardJS('.shortcode');

        clipboard.on('success', function(e) {
            // console.log(e); // For debugging
            setTooltip(e.trigger, 'Copied!');
            hideTooltip(e.trigger);
        });

        clipboard.on('error', function(e) {
            // console.log(e); // For debugging
            setTooltip(e.trigger, 'Failed!');
            hideTooltip(e.trigger);
        });


    });
    </script>

</div>
<?php }

1 Answer 1

0

I have managed to do the following. I made a clear cache button:

function advanza_direct_plugin_delete_transients() {
  delete_transient( 'advanza_direct_terms_of_service' );
  delete_transient( 'advanza_direct_privacy_policy' );
  // at the end redirect to target page
  exit( wp_redirect( admin_url( 'admin.php?page=advanza-direct-plugin-settings' ) ) );
}
add_action( 'admin_post_advanza_direct_plugin_delete_transients', 'advanza_direct_plugin_delete_transients' );

I put this where I have my admin logic:

<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
        <input type="hidden" name="action" value="advanza_direct_plugin_delete_transients">
        <?php submit_button( 'Clear cache', 'delete', '', false ); ?>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.