0

I'm creating a plugin with Titan Framework to handle the options pages. As part of the plugin, I collect a log of failed log-in attempts to WP and then display them in the options panel.

Every so often the log needs clearing out and so I have created an AJAX Button control that when clicked calls a function that wipes the log. However, inorder to show this process has taken place and that pressing the button actually did something, I need to refresh the page.

According to the documentation for Titan Framework it would appear that this can be done using the call back property of the button. It says:

success_callback | string/array | (Optional) The name of a Javascript function (within the scope of window) to call after a successful ajax call. Use admin_enqueue_scripts to add your scripts. Defaults to blank/none.

However there are no examples showing how this is actually done. Here's my code so far:

PLUGIN FILE(AJAX button):

$lsPanel->createOption( array(
  'name' => 'Reset Log',
  'type' => 'ajax-button',
  'action' => 'yyy_reset_security_logs',
  'label' => __( 'Reset Security Logs', 'default' ),
  'success_callback' => 'ajax_success_refresh',
) );

//run a clinical cms / titan framework ajax call
add_action( 'wp_ajax_yyy_reset_security_logs', array( $this, 'yyy_reset_logs' ) );

PLUGIN FILE(AJAX action function):

function yyy_reset_logs() {
    $result = $this->yyy_clear_security_logs();
    if ( $result ) {
        wp_send_json_success( __( 'Success!', 'default' ) );
        }
        wp_send_json_error( __( 'Failed!', 'default' ) );
    }

So now I want to refresh the page using a success callback. I've enqueued a simple script via admin_enqueue_scripts :

window.location.reload(false); 

The goal is to have this script refresh the page but only when the AJAX button is pressed and a success result is detected. At present, the script is loaded onloading any/all admin pages. The result being that admin pages never load fully due to an infinite JavaScript loop.

Any ideas how I can limit the refresh JS to only be run on a successful clearing of the logs?

1 Answer 1

1

So now I want to refresh the page using a success callback. I've enqueued a simple script via admin_enqueue_scripts:

I believe in that script, you need a global JS function named ajax_success_refresh, as in the following example:

// This is the 'success_callback' for the AJAX action of 'yyy_reset_security_logs'.
function ajax_success_refresh() {
    window.location.reload( false );
}

Hint: Since it's a global function, you should make the function name as unique as possible.

0

Your Answer

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

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