I wonder if anyone could advise me further on my problem. Part of my plugin stores log files for debugging purposes. I have successfully displayed them in a (div#log) in my admin page using jquery and wp_localise_script. I have a button to delete these logs but I am unsure how to process this. I have a feeling that ajax might come in handy here but not sure where to start.
Here is the relevant parts of my code:
admin_enqueue_scripts (action)
$args = array(get_option('wow_tweets_log'));//log files fetched from wp_options table
wp_enqueue_script('wow_tweet');//registered earlier on with jQuery dependency
wp_localize_script('wow_tweet', 'wow_vars', $args);
Admin Page
<tr><th scope="row"><strong>Debugging</strong></th><td>
<div id="debug" class="button-primary">Debug</div><!--debug button shows logs-->
<div id="hide_debug" class="button-secondary">Hide</div><!--debug button hides logs-->
<div id="clear_log" class="button-secondary">Empty Log</div><!--Press to delete logs-->
</td></tr>
<tr><th scope="row"></th><td><div id="log"><!--Logs show here--></div></td></tr>
Javascript
jQuery(document).ready(function() {
var debug_show = jQuery('#log').hide();//hides log by default
jQuery('#debug').click(function(){//on click shows logs files in div#log
for (var i = 0, l = wow_vars.length; i < l; i++) {
var data = wow_vars[i];
}
jQuery('#log').show().html(data);
});
jQuery('#hide_debug').click(function()
{
debug_show.hide();
});
});
Action to clear log
function clear_log(){
delete_option('wow_tweets_log');//am stuck on how to invoke this
/*die(); would go at the end if ajax used*/
}
add_action('clear_log','clear_log');
So far this script is working to show all the log files, now all I need is to delete them when clicking #clear_log. I know plugging a do_action on init will delete them as soon as the page loads, making my javascript useless so I guess the only option is ajax! Do I need to add another reference to wp_localize_script()? Any help would be appreciated.
Ajax in WordPress works by sending an HTTP post to /wp-admin/admin-ajax.php (by default) that then fires the corresponding hook. So, you attach some jquery to an event triggered by your delete button, which then posts to admin-ajax.php, which has an action, say, delete_my_options(), which actually runs the php to delete. Then, you have a function, called a callback, that runs on successful completion of the ajax request. You could use this to fade your #log div out for example.
In short, you have three steps, the action, the ajax and the callback. The action is triggered by a DOM event and attached to two hooks, wp_ajax_{action_name} and wp_ajax_nopriv_{action_name} (only if you want no logged in users to be able to do it). These fire when that action is posted to wp-admin/admin-ajax.php. The ajax is the php (usually) function hooked to them. The callback function is a javascript function that is fired when the ajax is successfully completed.
Step by step:
Step 1, in your js file
Step 2, in your functions.php or a plugin
Add this to the function that you enqueue your javascript from: (thanks @Milo)
Then add this to your functions.php or plugin:
Step 3, back in your js file