2016-03-09 81 views

回答

1

在提交表單或AJAX調用,無論您想了解更多

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

嗨伊戈爾。謝謝!你能給我一個這個代碼的Ajax例子嗎? – Gerard

+0

@Gerard更新了我的答案。試試這個 –

+0

謝謝伊戈爾!我如何稱呼它?你能給我一個與al按鈕或txt鏈接的例子嗎?我非常感謝 – Gerard

相關問題