Custom Ajax Refresh
-
Hi Tobias,
Just need some advice. I have your AJAX refresh plugin installed but wanted to adjust it so that rather then reload each table periodically, it reloads them when the tables are saved in the admin area.
Is this something that can be done? I have tweaked the plugin code below to see if this is good enough to go. Please do adjust/amend this or advise what is best to get this method to work.
It would make sense with me having a digital signage setup for the client and having multiple tables on one screen/slider.
Thanks. P.S. This plugin works already, I just wanted to tweak it to work differently and prevent Ajax call backs when not needed.<?php
/*
Plugin Name: TablePress Extension: Table AJAX refresh after save
Plugin URI: https://tablepress.org/extensions/table-ajax-refresh/
Description: Custom Extension for TablePress that allows a table to refresh itself via AJAX after saving
Version: 1.1
Author: Tobias Bäthge
Author URI: https://tobias.baethge.com/
*/
/*
* Register necessary Plugin Actions.
*/
add_action( 'wp_ajax_tablepress_refresh_table', 'tablepress_refresh_table_callback' );
add_action( 'wp_ajax_nopriv_tablepress_refresh_table', 'tablepress_refresh_table_callback' );
/**
* AJAX handler to refresh the table content.
*
* @since 1.1
*/
function tablepress_refresh_table_callback() {
if ( ! isset( $_POST['table_id'] ) ) {
wp_send_json_error( 'Table ID not provided.' );
}
$table_id = sanitize_text_field( $_POST['table_id'] );
// Retrieve and render the table content
$table = TablePress::$model_table->load( $table_id );
$options = TablePress::$model_table->load_options( $table_id );
$content = TablePress::$controller_table->render_table( $table, $options );
wp_send_json_success( $content );
}
/**
* Add AJAX refresh script to trigger after table save.
*/
function tablepress_ajax_refresh_after_save() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(document).on('click', '.button-primary', function() {
setTimeout(function() {
$.post(
ajaxurl,
{
action: 'tablepress_refresh_table',
table_id: $('#table-id').val()
},
function(response) {
if (response.success) {
$('#tablepress-preview-table').html(response.data);
}
}
);
}, 500); // Adjust timeout as needed
});
});
</script>
<?php
}
add_action('admin_footer', 'tablepress_ajax_refresh_after_save');
The topic ‘Custom Ajax Refresh’ is closed to new replies.