Button to clear transients

时间:2018-10-12 作者:Remzi Cavdar

我想在管理页面上添加一个按钮来清除我使用的瞬态。

请参见下面的我的代码:

<?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 个回复
最合适的回答,由SO网友:Remzi Cavdar 整理而成

我已经做到了以下几点。我做了一个清除缓存按钮:

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\' );
我把这个放在我有管理逻辑的地方:

<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>

结束