How to delete a file from WordPress plugin folder

I made a WordPress plugin. It uses a cached xml file in the plugin folder /cache/feed.xml. Now I want to make it possible for the admin to delete this file from the plugins option page. Button or text link. Any idea?

Related posts

1 comment

  1. On form submit or AJAX call, whichever you like more

    PHP

    add_action('wp_enqueue_scripts', 'my_unique_enqueue_assets');
    add_action('wp_ajax_my_cache_removal', 'my_cache_removal_ajax_handler');
    
    function my_unique_enqueue_assets()
    {
        wp_enqueue_script('my_script', plugins_url('/my_script.js', __FILE__), array('jquery'), false, true);
        wp_localize_script('my_script', 'my_script_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
    
    function my_cache_removal_ajax_handler()
    {
        if(file_exists (plugin_dir_path(__FILE__) . 'cache/feed.xml'))
        {
            unlink(plugin_dir_path(__FILE__) . 'cache/feed.xml')
        }
    }
    

    JS

    jQuery(document).ready(function ($) 
    {
        $('#clear_mah_cache').click(function()
        {
            $.ajax({type: "post", dataType: "json", url: my_script_ajax_object.ajax_url, data: {action: 'my_cache_removal'}}).done(function (e)
            {
                alert('You cache is kaput now');
            });
        });
    });
    

    HTML

    <button id="spr_reset_votes">Clear dat cache</button>
    

Comments are closed.